在记事本选项卡上更新时钟

时间:2013-06-29 04:06:23

标签: python

我正在通过Python创建一个应用程序,我正在创建的一个选项卡是显示当前时间:本地和GMT。当我启动程序时,它给了我程序启动的时间。我想知道是否有办法更新时间,以便我可以在应用程序上有实际的时间。我为我的时钟尝试了这种类型的代码:http://ygchan.blogspot.com/2012/05/python-how-to-make-clock-timer-in.html

到目前为止,这是我的应用程序:

    from Tkinter import *
    from ttk import *
    import time

    root = Tk()
    root.geometry('1000x900')
    root.title("Astronomical Observing Recording Program")
    frame1 = Frame(root, name='frame1')
    frame1.pack(fill=BOTH)


    localtime = time.asctime(time.localtime(time.time()))
    gmttime = time.asctime(time.gmtime(time.time()))

    welcome_message = "Hi" #shortened


   current_time = "The current time is: " + str(localtime)
   gmt_time = "The current GMT time is: " + str(gmttime)


   root.protocol("WM_DELETE_WINDOW", frame1.quit)
   nb = Notebook(frame1, name='nb')
   nb.pack(fill=BOTH, padx=2, pady=3)

   welcome_screen = Frame(nb, name='welcome_screen')
   Label(welcome_screen, text=welcome_message).pack(side=LEFT)
   nb.add(welcome_screen, text='Welcome')

   time_screen = Frame(frame1, name='time_screen')
   Label(time_screen, text=current_time).pack(side=TOP)
   Label(time_screen, text=gmt_time).pack(side=TOP)
   nb.add(time_screen, text='Time Bar')

   frame1.mainloop()

如果有人可以帮助我,或者甚至让我朝着正确的方向前进,那么我可以尝试自己修复它。

谢谢

1 个答案:

答案 0 :(得分:1)

使用after,它将在给定时间后调用该函数。

from Tkinter import *
from ttk import *
import time

root = Tk()
root.geometry('1000x900')
root.title("Astronomical Observing Recording Program")
frame1 = Frame(root, name='frame1')
frame1.pack(fill=BOTH)


welcome_message = "Hi" #shortened


root.protocol("WM_DELETE_WINDOW", frame1.quit)
nb = Notebook(frame1, name='nb')
nb.pack(fill=BOTH, padx=2, pady=3)

welcome_screen = Frame(nb, name='welcome_screen')
Label(welcome_screen, text=welcome_message).pack(side=LEFT)
nb.add(welcome_screen, text='Welcome')

time_screen = Frame(frame1, name='time_screen')
lb_current_time = Label(time_screen)
lb_current_time.pack(side=TOP)
lb_gmt_time = Label(time_screen)
lb_gmt_time.pack(side=TOP)
nb.add(time_screen, text='Time Bar')

def update_time():
    localtime = time.asctime(time.localtime(time.time()))
    gmttime = time.asctime(time.gmtime(time.time()))
    current_time = "The current time is: " + localtime
    gmt_time = "The current GMT time is: " + gmttime
    lb_current_time['text'] = current_time
    lb_gmt_time['text'] = gmt_time
    root.after(1000, update_time)
update_time()


frame1.mainloop()