Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python GUI倒计时计时器(不要使用类)_Python_User Interface_Tkinter_Timer_Countdown - Fatal编程技术网

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

python GUI倒计时计时器(不要使用类),python,user-interface,tkinter,timer,countdown,Python,User Interface,Tkinter,Timer,Countdown,我的源代码中只有一个问题,lbl_时间不会改变,除了这个,所有的东西都运行得很好。 我只能在我的程序中使用函数,如果有人能在这个函数程序中帮助我,请这样做 import tkinter as tk from datetime import timedelta import winsound 设置时间是一个函数,用于倒计时,我使用timedelta构建一个时间对象来简化操作 def main(): def set_time(hours:int=0, minutes:int=0, seco

我的源代码中只有一个问题,lbl_时间不会改变,除了这个,所有的东西都运行得很好。 我只能在我的程序中使用函数,如果有人能在这个函数程序中帮助我,请这样做

import tkinter as tk
from datetime import timedelta
import winsound
设置时间是一个函数,用于倒计时,我使用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()

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

  • 基本上,您应该在
    lbl\u时间
    声明中使用
    textvariable
    而不是
    text
    作为参数

  • 如果执行
    while循环
    ,则可能会导致无限循环,如果代码在那里分支,则可能需要一个增量

  • 请检查以下内容,您可能希望取消对其中一些行的注释,希望它能解决您的问题:

    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()
    

    我不明白这个限制。为什么不能使用类?我不是说自然解决方案需要类,但你暗示了一个我们一无所知的限制。@roganjosh,因为这是一种实践,并说不要使用类,兄弟