我正在尝试创建一个简单的计时器应用程序,在该应用程序中,用户按下启动按钮(或使用按键绑定),并且时钟不断更新,直到用户按下停止按钮(或使用按键绑定)为止,但是尝试创建这个,我陷入了无限循环。
我尝试创建一个布尔值标志,其中True指示结束更新,并使用while循环进行检查,但是程序最终陷入了无限循环。这一切都在python 3.x(特别是v3.6.7)中。 在提供的代码中,我在维护错误和错误上下文的同时,尽可能地减少了代码。我还删除了前面提到的按键绑定,因为它们不是问题的一部分。
import tkinter as tk
class cubeTimer():
def __init__(self):
##Defines parts of timer
self.start_time = 0.0
self.end_time = 0.0
self.difference = 0.0
def get_time(self,num):
##Gets time since epoch
if num == 1:
self.start_time = time.time()
elif num == 2:
self.end_time = time.time()
def get_difference(self):
##Finds difference bwteen start and end times
self.difference = self.end_time - self.start_time
return self.difference
class cubeGUI():
def __init__(self):
##Instance variables for later use
self.num = 0
self.time_difference = 0
self.flag = False
##Creates instance of timer class
self.timer = cubeTimer()
##Creates GUI
self.root = tk.Tk()
##Label to show the solve time
self.time_label = tk.Label(text='-',height=5,width=10)
self.time_label.grid(row=1,columnspan=2,sticky=tk.W)
##Button to start timer
self.start_button = tk.Button(text='Start',height=5,width=10,command=self.start_process)
self.start_button.grid(row=2,column=0)
##Button to end timer, initialised as disabled
self.end_button = tk.Button(text='End',state=tk.DISABLED,height=5,width=10,command=self.end_process)
self.end_button.grid(row=2,column=1)
def start_process(self):
##Sets variable
self.num = 1
##Calls timer to get time
self.timer.get_time(self.num)
##Configures necessary buttons
self.start_button.configure(state=tk.DISABLED)
self.end_button.configure(state=tk.NORMAL)
while self.flag == False:
self.time_difference = self.timer.get_difference()
self.time_label.configure(text=self.time_difference)
time.sleep(0.1)
def end_process(self):
##Sets variable
self.num = 2
##Calls timer to get time
self.timer.get_time(self.num)
##Updates flag
self.flag = True
##Configures necessary button
self.end_button.configure(state=tk.DISABLED)
##Calls time difference
self.time_difference = self.timer.get_difference()
##Puts it on screen
self.time_label.configure(text=self.time_difference)
myTimer = cubeGUI()
我希望程序每0.1秒(或任何给定时间段)更新一次时间标签,但是当遇到无限循环并陷入阻塞时,程序将冻结。
答案 0 :(得分:1)
您不能在GUI内部使用循环,因为它会干扰GUI主循环。相反,您必须构建程序以集成到主循环中。在tkinter中,这是通过after()
方法完成的。这是您的固定代码(据我所知,您正在尝试做什么),包括一个适当的类:
import tkinter as tk
class CubeTimer(tk.Frame):
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.timer = ' '
self.time_value = tk.IntVar()
##Label to show the solve time
time_label = tk.Label(self, textvariable=self.time_value,height=5,width=10)
time_label.grid(row=1,columnspan=2,sticky=tk.W)
##Button to start timer
self.start_button = tk.Button(self, text='Start',height=5,width=10,command=self.start_process)
self.start_button.grid(row=2,column=0)
##Button to end timer, initialised as disabled
self.end_button = tk.Button(self, text='End',state=tk.DISABLED,height=5,width=10,command=self.end_process)
self.end_button.grid(row=2,column=1)
def timer_tick(self):
self.time_value.set(self.time_value.get() + 1)
self.timer = self.after(1000, self.timer_tick) # schedule this method to be called again in 1,000 millisconds (1 second)
def start_process(self):
##Configures necessary buttons
self.start_button.configure(state=tk.DISABLED)
self.end_button.configure(state=tk.NORMAL)
self.timer_tick()
def end_process(self):
self.after_cancel(self.timer) # cancel the scheduled method call
##Configures necessary button
self.end_button.configure(state=tk.DISABLED)
self.start_button.configure(state=tk.NORMAL)
def main():
root = tk.Tk()
myTimer = CubeTimer(root)
myTimer.pack()
root.mainloop()
if __name__ == "__main__":
main()