2013-03-28 313 views
0

我希望把這個數字時鐘:數字時鐘在Python 3和Tkinter的

import sys  
from tkinter import * 
import time 

root = Tk() 
time1 = '' 
clock = Label(root, font=('times', 20, 'bold'), bg='green') 
clock.pack(fill=BOTH, expand=1) 

def tick(): 
    global time1 
    # get the current local time from the PC 
    time2 = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    if time2 != time1: 
     time1 = time2 
     clock.config(text=time2) 
     # calls itself every 200 milliseconds 
     # to update the time display as needed 
     # could use >200 ms, but display gets jerky 
    clock.after(200, tick) 

tick() 
root.mainloop() 

在此狀態欄:

status = Label(mGui, text="v1.0", bd=1, relief=SUNKEN, anchor=W) 
status.pack(side=BOTTOM, fill=X) 

有沒有辦法做到這一點? 謝謝大家誰想要幫助,我感謝它:)

+0

可能的重複[如何使用tkinter創建一個計時器](http://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter) – nbro

回答

0

Tkinter noob在這裏,但我不認爲你可以把時鐘標籤內的狀態標籤。不過您可以通過側把它們放在一起:

import sys  
from tkinter import * 
import time 

def tick(): 
    global time1 
    # get the current local time from the PC 
    time2 = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    if time2 != time1: 
     time1 = time2 
     clock.config(text=time2) 
     # calls itself every 200 milliseconds 
     # to update the time display as needed 
     # could use >200 ms, but display gets jerky 
    clock.after(200, tick) 

root = Tk() 
time1 = '' 

status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W) 
status.grid(row=0, column=0) 

clock = Label(root, font=('times', 20, 'bold'), bg='green') 
clock.grid(row=0, column=1) 

tick() 
root.mainloop() 
+0

爲什麼你選擇更新每一個200ms的?這是一個絕對的確定性,時間每秒只會改變一次,因爲你只能改變到秒的粒度。 –

0

通常情況下,我做一個狀態出來的幀,然後收拾我想在框架中顯示力所能及的事情。例如,您的時鐘可能打包在右側,您的狀態標籤可能打包在左側。然後,您可以將整個狀態欄框放在GUI的底部。

通常我寧願給使用面向對象式的例子,但這裏有適合你的問題從代碼的例子:

import sys  
from tkinter import * 
import time 

root = Tk() 

statusbar = Frame(root) 
statusbar.pack(side="bottom", fill="x", expand=False) 

time1 = '' 
clock = Label(root, font=('times', 20, 'bold'), bg='green') 

def tick(): 
    global time1 
    # get the current local time from the PC 
    time2 = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    if time2 != time1: 
     time1 = time2 
     clock.config(text=time2) 
     # calls itself every 200 milliseconds 
     # to update the time display as needed 
     # could use >200 ms, but display gets jerky 
    clock.after(200, tick) 

tick() 

status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W) 
status.pack(in_=statusbar, side=LEFT, fill=BOTH, expand=True) 
clock.pack(in_=statusbar, side=RIGHT, fill=Y, expand=False) 

root.mainloop() 
+0

好的,謝謝你的回答,你知道任何其他(更好)的方式把時鐘放在狀態欄的角落嗎? – TheQuiteStupidMan

+0

@ user2221042:你如何定義「更好」?這個解決方案有什麼問題? –

+0

我的意思是更優雅的解決方案與其他時鐘...目前我想讓狀態欄看起來像在Windows操作系統,但這將工作得很好,我需要;) – TheQuiteStupidMan

1

什麼是if語句?這是沒有必要的,因爲clock.after語句在clock.after()函數中直接調用tick()函數,它將更新您的時間字符串。

import sys  
from Tkinter import * 
import time 

def tick(): 
    # get the current local time from the PC 
    time_string = time.strftime('%H:%M:%S') 
    # if time string has changed, update it 
    clock.config(text=time_string) 
    clock.after(200, tick) 

root = Tk() 
clock = Label(root, font=('times', 20, 'bold'), bg='green') 
clock.grid(row=0, column=1) 
tick() 
root.mainloop() 

此外,請記住使用Tkinter(首都T)爲Python 2.7和tkinter(小寫噸)用於Python 3.0。