python - 当我在 tkinter 中多次按下开始和暂停按钮时,倒数计时器的速度会增加
问题描述
我有一个倒计时脚本,当我一次又一次按下开始和暂停按钮时,秒数会快速更新。
起初,我认为在开始和暂停按钮之间快速单击会加快秒数,因此我添加time.sleep(1)
了延迟单击不起作用的按钮之间的时间。
我该如何解决这个问题?
我的算法正确吗?或者还有其他更好的方法来进行倒计时吗?如果是,我该怎么做?
代码
from tkinter import *
PAUSE = False
HOUR, MINUTE, SECOND = 0, 0, 0
def start():
'''Command for START button'''
global PAUSE
PAUSE = False
start_button['state'] = 'disabled'
pause_button['state'] = 'normal'
reset_button['state'] = 'normal'
# time.sleep(1) to delay time between clicks of pause and start buttons
Counter()
def pause():
'''Command for PAUSE button'''
global PAUSE
PAUSE = True
start_button['state'] = 'normal'
pause_button['state'] = 'disabled'
def reset():
'''Command for RESET button'''
global HOUR, MINUTE, SECOND, PAUSE
PAUSE = True
start_button['state'] = 'normal'
pause_button['state'] = 'disabled'
reset_button['state'] = 'disabled'
Time['text'] = '00:00:00'
HOUR, MINUTE, SECOND = 0, 0, 0
def Counter():
'''Updating hour, minute and seconds'''
global HOUR, MINUTE, SECOND
if PAUSE is False:
if SECOND == 59:
if MINUTE == SECOND == 59:
HOUR += 1
if MINUTE == 59:
MINUTE = 0
else:
MINUTE += 1
SECOND = -1
SECOND += 1
Time.config(text='{}:{}:{}'.format(str(HOUR).zfill(2), str(MINUTE).zfill(2), str(SECOND).zfill(2)))
root.after(1000, Counter)
root = Tk()
root.title('COUNTER')
width, height = 342, 108
pos_x = root.winfo_screenwidth() // 2 - width // 2
pos_y = root.winfo_screenheight() // 2 - height // 2
root.geometry('{}x{}+{}+{}'.format(width, height, pos_x, pos_y))
Time = Label(root, fg='Black', text='00:00:00', font=("Helvetica", 40))
Time.pack(side='bottom')
start_button = Button(root, text='START', font=("Arial", 16), fg='black', width=8, command=start)
start_button.pack(side='left')
pause_button = Button(root, text='PAUSE', font=("Arial", 16), fg='black', width=8, state='disabled', command=pause)
pause_button.pack(side='left')
reset_button = Button(root, text='RESET', font=("Arial", 16), fg='black', width=10, state='disabled', command=reset)
reset_button.pack(side='left', fill='both')
root.mainloop()
解决方案
您的Counter
函数调用root.after(1000,Counter)
每次单击,并Time
在单击时立即修改标签。当您足够快地单击按钮时,您可以安排多个root.after
并添加秒数。
要修改当前脚本,您可以跟踪当前操作,并调用root.after_cancel
暂停该操作。
from tkinter import *
PAUSE = False
HOUR, MINUTE, SECOND = 0, 0, 0
action = None #keep track on current action and also avoid click spam
def start():
'''Command for START button'''
global PAUSE, action
PAUSE = False
...
if not action:
Counter()
def pause():
'''Command for PAUSE button'''
global PAUSE, action
...
if action:
root.after_cancel(action)
action = None
def reset():
'''Command for RESET button'''
global HOUR, MINUTE, SECOND, PAUSE, action
...
HOUR, MINUTE, SECOND = 0, 0, 0
action = None
def Counter():
'''Updating hour, minute and seconds'''
global HOUR, MINUTE, SECOND, action
if PAUSE is False:
if SECOND == 59:
if MINUTE == SECOND == 59:
HOUR += 1
if MINUTE == 59:
MINUTE = 0
else:
MINUTE += 1
SECOND = -1
SECOND += 1
Time.config(text='{}:{}:{}'.format(str(HOUR).zfill(2), str(MINUTE).zfill(2), str(SECOND).zfill(2)))
action = root.after(1000, Counter)
root = Tk()
...
start_button = Button(root, text='START', font=("Arial", 16), fg='black', width=8, command=lambda: root.after(1000,start)) #to avoid the instant second increment
推荐阅读
- java - 扩展类的 scala 特征是如何为 JVM 目标编译的?
- r - 为 r 中的列分配新值
- google-apps-script - 电子表格:“您无权调用 setNumberFormat”
- wpf - ScrollViewer 不适用于 wpf 中的 tabcontrol
- apache-camel - 阿帕奇骆驼日期
- ubuntu - 无法使用 Snapcraft 创建快照
- javascript - 物化导航栏下拉菜单不起作用
- javascript - 使用数组数组创建多对一关系
- json - 如何在 ASP MVC 的 Json 文件中追加记录?
- javascript - CONTAINER(位于 GROUP 内)内的 Sprite 不可见