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

时间:2018-11-26 17:09:42

标签: python user-interface tkinter timer countdown

我的源代码中只有一个问题,lbl_time不会改变,除此以外,其他所有东西都运行良好。 我只能在程序中使用函数,如果有人可以在此函数程序中为我提供帮助,请这样做。

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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

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