在创建tkinter倒计时时需要一些帮助

时间:2016-01-26 11:34:52

标签: python python-3.x time tkinter

我正在尝试在tkinter中创建一个计时器。它从设定时间开始倒计时,然后输出“时间到了”。我写了大部分代码。我也插入了一个日志记录系统,因此我可以处理一些错误,这些错误会记录到日志文件中。 代码:

from tkinter import *

import logging
logging.basicConfig(filename='timer_err.log',format='%(levelname)s:%(message)s',level=logging.DEBUG)
main = Tk()
main.wm_attributes("-fullscreen",True)
main.wm_attributes("-transparentcolor","darkgreen")
main.title("Timer")
main.config(background="orange")
timerun = 0
comp = 0
secs = DoubleVar()
mins = IntVar()
hrs = IntVar()
def ok():
    global timerun
    try:
        comp = float(float(secs.get())+int(mins.get()*60)+int(hrs.get()*3600))
        done.config(state="disabled")
        logging.info(("\t{} - Program launched").format(asctime(localtime())))
        while timerun < comp:
            timerun = clock()
            disp.config(state="normal")
            disp.insert(0, str(round(comp-timerun, 3)))
            main.update()
            disp.delete(0, END)
            disp.config(state="readonly")
        disp.config(state="normal")
        disp.insert(0, "Time is up!")
        disp.config(state="readonly")
        main.update()
        def end(self):
            main.destroy()
            logging.info(("\t{} - Program successfully terminated").format(asctime(localtime())))
        main.bind("<Escape>",end)
    except Exception as e:
        logging.debug(("\t{} - {}").format(asctime(localtime()),str(e)))
        logging.warning(("\t{} - {}").format(asctime(localtime()),str(e)))
        pass
    finally:
        logging.info(("\t{} - Program stopped").format(asctime(localtime())))

dispframe = Frame(main, bg="orange")
dispframe.pack(fill="both")
disp = Entry(dispframe, fg="black", bg="lightgray", font=("Arial", 100, "bold"), bd=0, state="readonly", takefocus=False,  readonlybackground="lightgray", selectforeground="white", selectbackground="navy")
disp.pack(fill="x", pady=12, side="left")
setframe = Frame(main, bg="orange")
setframe.pack(fill="both")
seconds = Spinbox(setframe, fg="red", bg="yellow", from_=0, to=59.5, increment=0.5, font=("Arial", 100, "bold"), bd=0, state="readonly", takefocus=True, readonlybackground="yellow", buttonbackground="green", selectforeground="white", selectbackground="navy", wrap=True, textvariable=secs)
seconds.pack(fill="x", pady=12)
minutes = Spinbox(setframe, fg="red", bg="yellow", from_=0, to=59, font=("Arial", 100, "bold"), bd=0, state="readonly", takefocus=True, readonlybackground="yellow", buttonbackground="green", selectforeground="white", selectbackground="navy", wrap=True, textvariable=mins)
minutes.pack(fill="x", pady=12)
hours = Spinbox(setframe, fg="red", bg="yellow", from_=0, to=99999, font=("Arial", 100, "bold"), bd=0, state="readonly", takefocus=True, readonlybackground="yellow", buttonbackground="green", selectforeground="white", selectbackground="navy", wrap=True, textvariable=hrs)
hours.pack(fill="x", pady=12)
okframe = Frame(main, bg="orange")
okframe.pack(fill="both")
done = Button(okframe, fg="red", bg="yellow", text="Done", font=("Arial", 100, "bold"), bd=0, activeforeground="red", activebackground="yellow", underline=1, command=ok)
done.pack(fill="x", pady=12)

from time import *
main.mainloop()

有人可以帮我创建一个“重置”按钮,将时间设置为0吗? 或者至少给我一些关于如何做到这一点的建议。谢谢。

0 个答案:

没有答案