为什么我的tkinter倒数计时器达到零时不会自动重新开始?

时间:2019-06-11 17:22:05

标签: python loops tkinter countdown

我正在尝试创建一个倒数计时器,该计时器在达到零时会自动重置,然后再次开始倒数。我已经基于另一个用户的代码做了一些细微的修改,但是我缺少了一些东西。

from tkinter import *
from tkinter import ttk
from tkinter import font
import time
import datetime

global endTime
global taktTime

def quit(*args):
    root.destroy()

def show_time():
    # Get the time remaining until the event
    remainder = endTime - datetime.datetime.now()
    # remove the microseconds part
    remainder = remainder - datetime.timedelta(microseconds=remainder.microseconds)
    if remainder.days >= 0:
        # Show the time left
        txt.set(remainder)
        # Trigger the countdown after 1000ms
        root.after(1000, show_time)
    else:
        txt.set('complete')
        endTime = datetime.datetime.now() + taktTime

# Use tkinter lib for showing the clock
root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("x", quit)
root.after(1000, show_time)

taktTime = datetime.timedelta(seconds=10)

# Set the end date and time for the countdown
endTime = datetime.datetime.now() + taktTime

fnt = font.Font(family='Helvetica', size=250, weight='bold')
txt = StringVar()
lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground="white", background="black")
lbl.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()

我试图在余数为零时重置endTime,但是出现错误“ UnboundLocalError:分配前引用了本地变量'endTime'”

我确定这是一个简单的初学者错误,所以在此先感谢您的帮助。

0 个答案:

没有答案