Python Tkinter中的实时时钟显示
我想使用Tkinter和时间库创建实时时钟。我创建了一个类,但不知何故我无法解决我的问题 我的代码Python Tkinter中的实时时钟显示,python,tkinter,Python,Tkinter,我想使用Tkinter和时间库创建实时时钟。我创建了一个类,但不知何故我无法解决我的问题 我的代码 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.
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()
的第二个参数应该是一个函数-当您提供any时-但您提供的是str对象。因此,您将得到一个错误
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()
还请注意:
对该方法的每次调用只调用一次回调。保存
调用回调时,需要在内部重新注册回调
本身
您应该提供您得到的stacktrace,或者描述您面临的问题。您是否收到错误消息?请把它寄出去好吗?