2016-05-14 140 views
-2

我想創建一個全屏時鐘在python,將顯示全屏使用tkinter。Python全屏時鐘與tkinter

到目前爲止,我有2位代碼工作,創建一個全屏tkinter窗口,並使用strftime打印時間。

試圖將2位代碼合併在一起正在做我的頭 - 我可以得到一個運行在shell中的時鐘,或者一個顯示時間但不更新的tkinter窗口。

我需要在tkinter窗口中顯示時間,並且每0.5秒更新一次時間。

我有什麼是以下 - 它遠沒有完成。 任何幫助非常感謝。 喬恩

import sys 
if sys.version_info[0] == 2: 
    from Tkinter import * 
    import Tkinter as tk 
else: 
    from tkinter import * 
    import tkinter as tk 

import time 
from time import * 
import os 
from os import * 

clock = tk.Tk() # make it cover the entire screen 
w, h = clock.winfo_screenwidth(), clock.winfo_screenheight() 
clock.overrideredirect(1) 
clock.geometry("%dx%d+0+0" % (w, h)) 
clock.focus_set() # <-- move focus to this widget 
clock.bind("<Escape>", lambda e: e.widget.quit()) 

###############this is the problem... 

while True: 
    time = strftime("%A, %d %B %Y %X") 
    sleep (0.5) 
    #print (time) 

###############this is the problem... 

text = Text(clock) 
text.insert(INSERT, time) 
text.pack() 
clock.mainloop() 
+0

在http://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter –

回答

0

使用after功能。

import sys 
if sys.version_info[0] == 2: 
    from Tkinter import * 
    import Tkinter as tk 
else: 
    from tkinter import * 
    import tkinter as tk 

from time import * 
import os 
from os import * 

clock = tk.Tk() # make it cover the entire screen 
w, h = clock.winfo_screenwidth(), clock.winfo_screenheight() 
clock.overrideredirect(1) 
clock.geometry("%dx%d+0+0" % (w, h)) 
clock.focus_set() # <-- move focus to this widget 
clock.bind("<Escape>", lambda e: e.widget.quit()) 

time = strftime("%A, %d %B %Y %X") 

def getTime(): 
    time = strftime("%A, %d %B %Y %X") 
    text.delete('1.0', END)     #delete everything 
    text.insert(INSERT, time)    #inser new time 
    clock.after(500, getTime)    #wait 0.5 sec and go again 

text = Text(clock) 
text.insert(INSERT, time) 
text.pack() 
clock.after(500, getTime) 
clock.mainloop() 
+0

看看哦光澤。我喜歡。謝謝。下一階段是混亂格式化和其他一些小部件,如顯示基於日期的星期編號。可能會在聖誕節結束...... :-p – Jon

+0

我相信你會管理。 BTW。如果你喜歡答案,接受它。 =) –

+0

我認爲我接受了它。第一次IVE不得不吞下我的驕傲,並使用stackoverflow ... – Jon