本文介绍了Tkinter 中的实时时钟显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Tkinter 和时间库创建实时时钟.我创建了一个类,但不知何故我无法弄清楚我的问题.
I want to create real time clock using Tkinter and time library. I have created a class but somehow I am not able to figure out my problem.
我的代码
from tkinter import *
import time
root = Tk()
class Clock:
def __init__(self):
self.time1 = ''
self.time2 = time.strftime('%H:%M:%S')
self.mFrame = Frame()
self.mFrame.pack(side=TOP,expand=YES,fill=X)
self.watch = Label (self.mFrame, text=self.time2, font=('times',12,'bold'))
self.watch.pack()
self.watch.after(200,self.time2)
obj1 = Clock()
root.mainloop()
推荐答案
after()
应该是一个 function
- 当你给出 any 时 - 但你给出的是一个 str
对象.因此,您收到错误消息.
Second parameter of after()
should be a function
-when you are giving any- but you are giving a str
object. Hence you are getting an error.
from tkinter import *
import time
root = Tk()
class Clock:
def __init__(self):
self.time1 = ''
self.time2 = time.strftime('%H:%M:%S')
self.mFrame = Frame()
self.mFrame.pack(side=TOP,expand=YES,fill=X)
self.watch = Label(self.mFrame, text=self.time2, font=('times',12,'bold'))
self.watch.pack()
self.changeLabel() #first call it manually
def changeLabel(self):
self.time2 = time.strftime('%H:%M:%S')
self.watch.configure(text=self.time2)
self.mFrame.after(200, self.changeLabel) #it'll call itself continuously
obj1 = Clock()
root.mainloop()
还要注意:
每次调用此方法只调用一次回调.保持调用回调,需要在里面重新注册回调自己.
这篇关于Tkinter 中的实时时钟显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!