Python全屏时钟与tkinter

时间:2016-05-14 17:41:58

标签: 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()

1 个答案:

答案 0 :(得分: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()