如何使用tkinter更新多个标签



我正在尝试在python中制作秒表,该秒表还显示以十进制小时为worktime的经过时间,并将经过的任何时间转换为overtime

我得到了秒表部分功能(公平地说,我主要模仿教程),但当我试图添加额外的小数,然后事情出错了。我尝试了一堆看起来合乎逻辑的东西,但在运行时得到了更多的错误,或者我的标签在秒表停留在零,或者在目前的情况下,几秒钟后python崩溃(我的猜测是三个.after基本上是指数级调用运行update()?)

我想推土机解决方案对我来说只是为我的每个标签复制update()函数,我有9/10的信心它会工作,但这听起来愚蠢的冗余。

import tkinter as tk
### Vars
counting = False
hours, minutes, seconds = 0, 0, 0
totalWorktime, overtime = 0, 0
### Funcs
def start():
global counting
if not counting:
update()
counting = True
def pause():
global counting
if counting:
stopwatch_label.after_cancel(update_time)
worktime_label.after_cancel(update_worktime)
overtime_label.after_cancel(update_overtime)
counting = False
def reset():
global counting
if counting:
stopwatch_label.after_cancel(update_time)
worktime_label.after_cancel(update_worktime)
overtime_label.after_cancel(update_overtime)
counting = False
global hours, minutes, seconds, totalWorktime, overtime
hours, minutes, seconds = 0, 0, 0
totalWorktime, overtime = 0, 0
stopwatch_label.config(text='00:00:00')
# update stopwatch label
def update():
global hours, minutes, seconds, totalWorktime, overtime
# time counting system
seconds += 1
if seconds ==60:
minutes += 1
seconds = 0
if minutes == 60:
hours += 1
minutes = 0
totalWorktime = hours + (minutes / 60)
if totalWorktime > 8:
overtime = totalWorktime - 8
# format with zero padding
hours_string = f'{hours}' if hours > 9 else f'0{hours}'
minutes_string = f'{minutes}' if minutes > 9 else f'0{minutes}'
seconds_string = f'{seconds}' if seconds > 9 else f'0{seconds}'
# building label
stopwatch_label.config(text=hours_string + ':' + minutes_string + ':' + seconds_string)
global update_time, update_worktime, update_overtime
update_time = stopwatch_label.after(10, update)
update_worktime = worktime_label.after(10, update)
update_overtime = overtime_label.after(10, update)
### UI
# main window
root = tk.Tk()
root.geometry('512x256')
root.title('Worktime Stopwatch')
# time display
stopwatch_label = tk.Label(text='00:00:00', font=('Arial', 80))
stopwatch_label.pack()
worktime_label = tk.Label(text='Work time : ' + str(totalWorktime), font=('Arial', 20))
worktime_label.pack()
overtime_label = tk.Label(text='Overtime : ' + str(overtime), font=('Arial', 20))
overtime_label.pack()
# buttons
start_button = tk.Button(text='Start', height=4, width=8, font=('Arial', 20), command=start)
start_button.pack(side=tk.LEFT)
pause_button = tk.Button(text='Pause', height=4, width=8, font=('Arial', 20), command=pause)
pause_button.pack(side=tk.LEFT)
reset_button = tk.Button(text='Reset', height=4, width=8, font=('Arial', 20), command=reset)
reset_button.pack(side=tk.LEFT)
# run
root.mainloop()
PS:是的,我知道我可以使用时间模块,我只是喜欢简单有趣的数学在我的脚本。
PS2:是的,我知道我现在的秒表计数时间快了100倍,这是为了便于测试。当它工作时,我会回到1000ms。

感谢@acw1668的指出,我重新审视了我的代码,并注意到我是如何误解了.after()的用法,并期望它负责刷新标签,而它实际上所做的只是以固定的间隔调用update()

重要的部分就在上面两行,在我的"构建标签"下。备注:

stopwatch_label.config(text=hours_string + ':' + minutes_string + ':' + seconds_string)

出于某种原因,我的大脑认为这只是将秒表标签的不同部分组合成一个。但.config()的核心用途恰恰是改变文本。

所以我所要做的就是复制这一行两次,并改变它来影响我的worktimeovertime计数器:
worktime_label.config(text='Work time : ' + str(totalWorktime))
overtime_label.config(text='  Overtime : ' + str(overtime))

我还必须删除reset()pause()中各自的.after_cancel()方法,经过一些修饰性的更改,这里是整个功能代码:

import tkinter as tk
### Vars
counting = False
hours, minutes, seconds = 0, 0, 0
totalWorktime, overtime = 0, 0
### Funcs
def start():
global counting
if not counting:
update()
counting = True
def pause():
global counting
if counting:
stopwatch_label.after_cancel(update_time)
counting = False
def reset():
global counting
if counting:
stopwatch_label.after_cancel(update_time)
counting = False
global hours, minutes, seconds, totalWorktime, overtime
hours, minutes, seconds = 0, 0, 0
totalWorktime, overtime = 0, 0
stopwatch_label.config(text='00:00:00')
worktime_label.config(text='Work time : 0')
overtime_label.config(text='  Overtime : 0')
# update stopwatch label
def update():
global hours, minutes, seconds, totalWorktime, overtime
# time counting system
seconds += 1
if seconds ==60:
minutes += 1
seconds = 0
if minutes == 60:
hours += 1
minutes = 0
totalWorktime = round(hours + (minutes / 60), 2)
if totalWorktime > 8:
overtime = totalWorktime - 8
# format with zero padding
hours_string = f'{hours}' if hours > 9 else f'0{hours}'
minutes_string = f'{minutes}' if minutes > 9 else f'0{minutes}'
seconds_string = f'{seconds}' if seconds > 9 else f'0{seconds}'
# refresh labels with new text content
stopwatch_label.config(text=hours_string + ':' + minutes_string + ':' + seconds_string)
worktime_label.config(text='Work time : ' + str(totalWorktime))
overtime_label.config(text='  Overtime : ' + str(overtime))
global update_time, update_worktime, update_overtime
update_time = stopwatch_label.after(1000, update)
### UI
# main window
root = tk.Tk()
root.geometry('512x256')
root.title('Worktime Stopwatch')
# time display
stopwatch_label = tk.Label(text='00:00:00', font=('Arial', 80))
stopwatch_label.pack()
worktime_label = tk.Label(text='Work time : 0', font=('Arial', 20), anchor="w", width=20)
worktime_label.pack()
overtime_label = tk.Label(text='  Overtime : 0', font=('Arial', 20), anchor="w", width=20)
overtime_label.pack()
# buttons
start_button = tk.Button(text='Start', height=4, width=8, font=('Arial', 20), command=start)
start_button.pack(side=tk.LEFT)
pause_button = tk.Button(text='Pause', height=4, width=8, font=('Arial', 20), command=pause)
pause_button.pack(side=tk.LEFT)
reset_button = tk.Button(text='Reset', height=4, width=8, font=('Arial', 20), command=reset)
reset_button.pack(side=tk.LEFT)
# run
root.mainloop()

谢谢你的帮助!

相关内容

  • 没有找到相关文章

最新更新