【发布时间】:2016-04-19 09:55:06
【问题描述】:
我正在尝试为我的反应游戏构建用户界面。我已经在彼此之上设置了框架,但是当我将秒表功能添加到其中一个框架上时,开始按钮不起作用。我已经测试了重置按钮和退出按钮,它们工作正常。我无法测试暂停按钮,因为秒表没有启动。谁能帮帮我?
这是框架和秒表的代码:
class LvlOneR(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Level One",
background='white',
foreground='dodger blue',
font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
#stopwatch
def update_timeText():
if (state):
global timer
# Every time this function is called,
# we will increment 1 centisecond (1/100 of a second)
timer[2] += 1
# Every 100 centisecond is equal to 1 second
if (timer[2] >= 100):
timer[2] = 0
timer[1] += 1
# Every 60 seconds is equal to 1 min
if (timer[1] >= 60):
timer[0] += 1
timer[1] = 0
# We create our time string here
timeString = pattern.format(timer[0], timer[1], timer[2])
# Update the timeText Label box with the current time
timeText.configure(text=timeString)
# Call the update_timeText() function after 1 centisecond
self.after(10, update_timeText)
# To start game
def start():
global state
state = True
# To pause the game
def pause():
global state
state = False
# To reset the timer to 00:00:00
def reset():
global timer
timer = [0, 0, 0]
timeText.configure(text='00:00:00')
# To leave game our program
def leave():
controller.show_frame("ReactionGame")
# Simple status flag
# False mean the timer is not running
# True means the timer is running (counting)
state = False
# Our time structure [min, sec, centsec]
timer = [0, 0, 0]
# The format is padding all the
pattern = '{0:02d}:{1:02d}:{2:02d}'
# Create a timeText Label (a text box)
timeText = tk.Label(self, text="00:00:00",
background='white',
foreground='dodger blue',
font=("Helvetica", 150))
startButton = tk.Button(self, text='Start', command=start)
pauseButton = tk.Button(self, text='Pause', command=pause)
resetButton = tk.Button(self, text='Reset', command=reset)
quitButton = tk.Button(self, text='Quit', command=leave)
timeText.pack()
startButton.pack()
pauseButton.pack()
resetButton.pack()
quitButton.pack()
update_timeText()
这是设置框架的代码:
class Game(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (LoginScreen, MainMenu, ReactionGame, MemoryGame, HighScores, LvlOneR):
page_name = F.__name__
frame = F(container, self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
frame.configure(background='white')
self.show_frame("LoginScreen")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
【问题讨论】: