【问题标题】:Python fullscreen clock with tkinter [duplicate]带有tkinter的Python全屏时钟[重复]
【发布时间】:2016-05-14 17:41:58
【问题描述】:

我正在尝试在 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()

【问题讨论】:

标签: python tkinter


【解决方案1】:

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

【讨论】:

  • 哦,闪亮。我喜欢。谢谢。下一个阶段是处理格式和其他一些零碎的东西,例如根据日期显示周数。可能在圣诞节前完成... :-p
  • 我相信你会做到的。顺便提一句。如果你喜欢这个答案,接受它。 =)
  • 我想我已经接受了。我第一次不得不放弃我的骄傲并使用stackoverflow......
  • 标记为重复时现在不能这样做,寻求帮助并没有错。我们都需要它。
  • 看来.overrideredirect(1) 是一个已知的 Tcl/Tk 错误。见stackoverflow.com/questions/5886280/…
猜你喜欢
  • 2016-06-24
  • 1970-01-01
  • 2016-08-17
  • 2012-04-13
  • 2019-04-17
  • 1970-01-01
  • 2010-10-11
  • 2020-07-14
  • 1970-01-01
相关资源
最近更新 更多