python GUI 倒计时器(不要使用 类)

python GUI countdown timer (don't use classes)

我的源代码中只有一个问题,lbl_time 不会改变,除此之外一切都很好 运行。 我只能在我的程序中使用函数,如果有人可以在这个函数程序中帮助我,请帮助我。

import tkinter as tk
from datetime import timedelta
import winsound

set time是一个倒计时的函数,我用timedelta建立一个时间对象来简单操作

def main():
    def set_time(hours:int=0, minutes:int=0, seconds:int=0):
        end = timedelta(hours=hours, minutes=minutes, seconds=seconds)
        one_second = timedelta(seconds=1)
        result = end - one_second
        new_time = seconds_to_hms(result.seconds)
        if result.seconds is 0:
            while True:
                try:
                    winsound.PlaySound("Ringtones\1.cookie clock.wav", winsound.SND_FILENAME)
                except KeyboardInterrupt:
                    break
        else:
            hours, minutes, seconds = new_time.get('hours'), new_time.get('minutes'), new_time.get('seconds')
            time.set(str(hours)+':'+str(minutes)+':'+str(seconds))
            root.update()
            root.after(1000, lambda : set_time(hours, minutes, seconds))

    def seconds_to_hms(seconds:int) -> dict:
        hours, minutes, seconds = 0, 0, seconds
        if seconds >= 3600:
            hours = seconds//3600
            seconds = seconds - hours*3600
        if seconds >= 60:
            minutes = seconds//60
            seconds = seconds - minutes*60
        result = {'hours': hours, 'minutes': minutes, 'seconds': seconds}
        return result

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

    root = tk.Tk()
    root.title(string='Timer')
    time = tk.StringVar()
    root.configure(background='black')
    logo = tk.PhotoImage(file='Logo.png')
    lbl_logo = tk.Label(root, image=logo, bg='black').pack(side='right')
    lbl_timer = tk.Label(root, padx=10, text="Timer", fg='white', bg='black', font='Times 24', anchor='center').pack()
    lbl_time = tk.Label(root, text=time, font="Times 38", fg='white', bg='black').pack()
    btn_start = tk.Button(root, text='start', bg='gray', fg='black', command=lambda : set_time()).pack()
    root.bind('x', quit)
    root.after(1000, lambda : set_time(0,0, 3))
    root.mainloop()




if __name__ == '__main__':
    main()

花了一些时间来理解你的问题,但我可能已经解决了你的问题。

  1. 从根本上说,您应该在 lbl_time 声明中使用 textvariable 而不是 text 作为参数。

  2. 如果你的while loop被执行,它可能会导致无限循环,如果代码分支到那里你可能想要一个增量。

请检查以下内容,您可能想取消注释某些行,希望它能解决您的问题:

import tkinter as tk
from datetime import timedelta
import winsound
def main():
    def set_time(hours:int=0, minutes:int=0, seconds:int=0):
        end = timedelta(hours=hours, minutes=minutes, seconds=seconds)
        one_second = timedelta(seconds=1)
        result = end - one_second
        new_time = seconds_to_hms(result.seconds)
        if result.seconds is 0:
            while True:
                try:
                    winsound.PlaySound("Ringtones\1.cookie clock.wav", winsound.SND_FILENAME)
                except KeyboardInterrupt:
                    break
        else:
            hours, minutes, seconds = new_time.get('hours'), new_time.get('minutes'), new_time.get('seconds')
            time.set(str(hours)+':'+str(minutes)+':'+str(seconds))
            root.update()
            root.after(1000, lambda : set_time(hours, minutes, seconds))

    def seconds_to_hms(seconds:int) -> dict:
        hours, minutes, seconds = 0, 0, seconds
        if seconds >= 3600:
            hours = seconds//3600
            seconds = seconds - hours*3600
        if seconds >= 60:
            minutes = seconds//60
            seconds = seconds - minutes*60
        result = {'hours': hours, 'minutes': minutes, 'seconds': seconds}
        return result

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

    root = tk.Tk()
    root.title(string='Timer')
    time = tk.StringVar()
    root.configure(background='black')
  #  logo = tk.PhotoImage(file='Logo.png')
  #  lbl_logo = tk.Label(root, image=logo, bg='black').pack(side='right')
    lbl_timer = tk.Label(root, padx=10, text="Timer", fg='white', bg='black', font='Times 24', anchor='center').pack()
    lbl_time = tk.Label(root, textvariable=time, font="Times 38", fg='white', bg='black').pack() #changed text to textvariable
    btn_start = tk.Button(root, text='start', bg='gray', fg='black', command=lambda :set_time(0,0,3700)).pack()
    root.bind('x', quit)
    #root.after(1000, lambda : set_time(0,0, 3))
    root.mainloop()


if __name__ == '__main__':
    main()