I'm trying to write a little program in python using tkinter
, to count the number of times the mouse clicks a button in 60 sec, but I have a problem: I can't break the countdown loop.
我正在尝试使用tkinter在python中编写一个小程序,计算鼠标在60秒内单击按钮的次数,但我有一个问题:我无法打破倒计时循环。
Below is my code:
以下是我的代码:
from tkinter import *
class Application(Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.pack()
self.bttn_clicks = 0
self.createWidgets()
def createWidgets(self):
self.labelvariable = StringVar()
self.labelvariable.set("60")
self.thelabel = Label(self,textvariable = self.labelvariable,font=('Helvetica',50))
self.thelabel.pack(side=TOP)
self.firstButton = Button(self, text="Start", command=self.update_count)
self.firstButton.pack(side=TOP)
def update_count(self):
self.bttn_clicks += 1
self.firstButton["text"] = "Counter: " + str(self.bttn_clicks)
if self.bttn_clicks == 1:
countdown(1559)
def countdown(timeInSeconds):
mins, secs = divmod(timeInSeconds, 60)
timeformat = "{1:02d}".format(mins, secs)
app.labelvariable.set(timeformat)
root.after(1000, countdown, timeInSeconds-1)
if __name__ == '__main__':
root = Tk()
root.title("Timer")
app = Application(root)
root.mainloop()
1 个解决方案
#1
0
What I would do is keep everything within the class. Using an outside function just makes things harder to manage.
我要做的是保持课堂上的一切。使用外部函数只会使事情变得更难管理。
That said you should use IntVar()
instead of string var as this will aid us in keeping track of the time.
那说你应该使用IntVar()而不是string var,因为这将有助于我们跟踪时间。
My below code will check first if the timer is at 60. If so then start count down and add to counter. When the counter reaches zero the button has its command disabled and it no longer adds to the counter.
我的下面代码将首先检查定时器是否为60.如果是,则开始倒计时并添加到计数器。当计数器达到零时,按钮的命令被禁用,它不再添加到计数器。
The other thing I changed was adding a manager method for the timer. Because we are now using IntVar()
all we need to do is a get()
command followed by a -1
and an after()
statement to keep the timer running until zero.
我改变的另一件事是为计时器添加一个管理器方法。因为我们现在正在使用IntVar(),所以我们需要做的是一个get()命令后跟一个-1和一个after()语句来保持计时器运行直到零。
I also cleaned up your code a little to follow the PEP8 standard.
我还清理了一些代码,以遵循PEP8标准。
import tkinter as tk
class Application(tk.Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.bttn_clicks = 0
self.labelvariable = tk.IntVar()
self.create_widgets()
def create_widgets(self):
self.labelvariable.set(60)
self.thelabel = tk.Label(self, textvariable=self.labelvariable, font=('Helvetica',50))
self.thelabel.pack(side=tk.TOP)
self.firstButton = tk.Button(self, text="Start", command=self.update_count)
self.firstButton.pack(side=tk.TOP)
def update_count(self):
if self.labelvariable.get() == 60:
self.manage_countdown()
if self.labelvariable.get() != 0:
self.bttn_clicks += 1
self.firstButton.config(text="Counter: {}".format(self.bttn_clicks))
else:
self.firstButton.config(command=None)
def manage_countdown(self):
if self.labelvariable.get() != 0:
self.labelvariable.set(self.labelvariable.get() - 1)
self.after(1000, self.manage_countdown)
if __name__ == '__main__':
root = tk.Tk()
root.title("Timer")
app = Application(root).pack()
root.mainloop()
#1
0
What I would do is keep everything within the class. Using an outside function just makes things harder to manage.
我要做的是保持课堂上的一切。使用外部函数只会使事情变得更难管理。
That said you should use IntVar()
instead of string var as this will aid us in keeping track of the time.
那说你应该使用IntVar()而不是string var,因为这将有助于我们跟踪时间。
My below code will check first if the timer is at 60. If so then start count down and add to counter. When the counter reaches zero the button has its command disabled and it no longer adds to the counter.
我的下面代码将首先检查定时器是否为60.如果是,则开始倒计时并添加到计数器。当计数器达到零时,按钮的命令被禁用,它不再添加到计数器。
The other thing I changed was adding a manager method for the timer. Because we are now using IntVar()
all we need to do is a get()
command followed by a -1
and an after()
statement to keep the timer running until zero.
我改变的另一件事是为计时器添加一个管理器方法。因为我们现在正在使用IntVar(),所以我们需要做的是一个get()命令后跟一个-1和一个after()语句来保持计时器运行直到零。
I also cleaned up your code a little to follow the PEP8 standard.
我还清理了一些代码,以遵循PEP8标准。
import tkinter as tk
class Application(tk.Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.bttn_clicks = 0
self.labelvariable = tk.IntVar()
self.create_widgets()
def create_widgets(self):
self.labelvariable.set(60)
self.thelabel = tk.Label(self, textvariable=self.labelvariable, font=('Helvetica',50))
self.thelabel.pack(side=tk.TOP)
self.firstButton = tk.Button(self, text="Start", command=self.update_count)
self.firstButton.pack(side=tk.TOP)
def update_count(self):
if self.labelvariable.get() == 60:
self.manage_countdown()
if self.labelvariable.get() != 0:
self.bttn_clicks += 1
self.firstButton.config(text="Counter: {}".format(self.bttn_clicks))
else:
self.firstButton.config(command=None)
def manage_countdown(self):
if self.labelvariable.get() != 0:
self.labelvariable.set(self.labelvariable.get() - 1)
self.after(1000, self.manage_countdown)
if __name__ == '__main__':
root = tk.Tk()
root.title("Timer")
app = Application(root).pack()
root.mainloop()