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.
My code
from tkinter import *import timeroot = 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()
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 timeroot = 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 manuallydef 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 continuouslyobj1 = Clock()
root.mainloop()
Also note that:
The callback is only called once for each call to this method. To keepcalling the callback, you need to reregister the callback insideitself.