TKINTER STOPWATCH停止以1秒的延迟

发布于 2025-02-13 20:02:22 字数 2435 浏览 1 评论 0原文

当您按“重置”按钮时,秒表应该停止并重置,而是将数字设置为零,然后再添加一秒钟,并以00:00:0:0 1 结束。

与“停止”按钮相同。 FE如果我们在00:05停止停止,则停止使用00:06。加上一秒钟。但是有时您可以在当前第二秒的开始时在正确的时刻单击,以免发生。

我尝试了after_cancel,没有帮助。

在VSCODE中运行调试是不可能的,因为时间在时间​​时break点块按钮,所以我无法触发该功能。

我遵循了几个教程,每次实施它们的代码时,我都会得到这个问题。

为什么会发生?这1秒从哪里来以及如何修复?

from tkinter import *

root = Tk()

switch = False

def start():
    global switch
    switch = True

def stop():
    global switch
    switch = False


# Stopwatch variables
seconds = 0
minutes = 0
hours = 0

# Stopwatch functions

def stopwatch_update():
            global seconds, minutes, hours
            seconds += 1
            if seconds == 60:
                minutes += 1
                seconds = 0
            if minutes == 60:
                hours += 1
                minutes = 0
            
            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}'

            stopwatch_label.config(text = hours_string + ':' + minutes_string + ':' + seconds_string)
            if switch:
                stopwatch_label.after(1000, stopwatch_update)

def stopwatch_func(command):
    if command == 'start':
        start()
        stopwatch_start.config(state='disabled')
        stopwatch_stop.config(state='normal')
        stopwatch_reset.config(state='normal')
        stopwatch_update()


    if command == 'stop':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()

    if command == 'reset':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()
        global hours, minutes, seconds
        hours, minutes, seconds = 0, 0, 0
        stopwatch_label.config(text='00:00:00')

                

# Stopwatch components
stopwatch_label = Label(root, font='calibri 20', text='Stopwatch')
stopwatch_label.pack()
stopwatch_start = Button(root, text='Start', command=lambda: stopwatch_func('start'))
stopwatch_start.pack()
stopwatch_stop = Button(root, text='Stop', state='disabled',command=lambda:stopwatch_func('stop'))
stopwatch_stop.pack()
stopwatch_reset = Button(root, text='Reset', state='disabled', command=lambda:stopwatch_func('reset'))
stopwatch_reset.pack()


mainloop()

When you press "reset" button, the stopwatch is supposed to stop and reset, but instead it sets digits to zero, then adds one more second and ends up with 00:00:01.

The same with "stop" button. F.e. if we press stop at 00:05, it stops with 00:06. Plus one second every time. But sometimes you can click at the right moment, at the very beginning of the current second so it won't happen.

I tried after_cancel, didn't help.

It's impossible to run debugging in VScode because break points block buttons while the time is going, so I can't trigger the function.

I followed several tutorials and every time I implement pieces of their code, I get this issue.

Why does it happen? Where this 1 second comes from and how to fix?

from tkinter import *

root = Tk()

switch = False

def start():
    global switch
    switch = True

def stop():
    global switch
    switch = False


# Stopwatch variables
seconds = 0
minutes = 0
hours = 0

# Stopwatch functions

def stopwatch_update():
            global seconds, minutes, hours
            seconds += 1
            if seconds == 60:
                minutes += 1
                seconds = 0
            if minutes == 60:
                hours += 1
                minutes = 0
            
            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}'

            stopwatch_label.config(text = hours_string + ':' + minutes_string + ':' + seconds_string)
            if switch:
                stopwatch_label.after(1000, stopwatch_update)

def stopwatch_func(command):
    if command == 'start':
        start()
        stopwatch_start.config(state='disabled')
        stopwatch_stop.config(state='normal')
        stopwatch_reset.config(state='normal')
        stopwatch_update()


    if command == 'stop':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()

    if command == 'reset':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()
        global hours, minutes, seconds
        hours, minutes, seconds = 0, 0, 0
        stopwatch_label.config(text='00:00:00')

                

# Stopwatch components
stopwatch_label = Label(root, font='calibri 20', text='Stopwatch')
stopwatch_label.pack()
stopwatch_start = Button(root, text='Start', command=lambda: stopwatch_func('start'))
stopwatch_start.pack()
stopwatch_stop = Button(root, text='Stop', state='disabled',command=lambda:stopwatch_func('stop'))
stopwatch_stop.pack()
stopwatch_reset = Button(root, text='Reset', state='disabled', command=lambda:stopwatch_func('reset'))
stopwatch_reset.pack()


mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木森分化 2025-02-20 20:02:22

定义了stopwatch_label.fter(1000,stopwatch_update)的变量,现在一切正常。

stopwatch_update()的结尾:

if switch:
          global stopwatch_after
          stopwatch_after = stopwatch_label.after(1000, stopwatch_update)

stopwatch_func(命令)也已更新,以“ stop”和“ repent”命令:

if command == 'stop':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()
        stopwatch_label.after_cancel(stopwatch_after)

if command == 'reset':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()
        global hours, minutes, seconds
        hours, minutes, seconds = 0, 0, 0
        stopwatch_label.config(text='00:00:00')
        stopwatch_label.after_cancel(stopwatch_after)

Defined a variable for stopwatch_label.after(1000, stopwatch_update) and now everything works fine.

The end of stopwatch_update():

if switch:
          global stopwatch_after
          stopwatch_after = stopwatch_label.after(1000, stopwatch_update)

stopwatch_func(command) is also updated for "stop" and "reset" commands:

if command == 'stop':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()
        stopwatch_label.after_cancel(stopwatch_after)

if command == 'reset':
        stopwatch_start.config(state='normal')
        stopwatch_stop.config(state='disabled')
        stop()
        global hours, minutes, seconds
        hours, minutes, seconds = 0, 0, 0
        stopwatch_label.config(text='00:00:00')
        stopwatch_label.after_cancel(stopwatch_after)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文