2013-06-29 75 views
1

我正在通過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

使用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() 
+0

修好了!謝謝您的幫助! –

相關問題