May-14-2020, 05:58 PM
Here's a simple clock that uses after command:
import tkinter as tk from time import strftime class Clock: def __init__(self, parent=None): # following allows iportiing class into another GUI framework. if not parent: self.parent = tk.Tk() else: self.parent = parent self.parent.title('My Clock') self.parent.geometry('200x50+10+10') self.clock = tk.Label(self.parent, borderwidth = 2, height=2, relief=tk.SOLID) self.clock.pack(padx=5, pady=2, fill=tk.BOTH) self.clock['text'] = strftime("%H:%M:%S") self.time_now = strftime("%H:%M:%S") self.increnment_time() if not parent: self.parent.mainloop() def display_time(self): self.clock['text'] = strftime("%H:%M:%S") def increnment_time(self): self.display_time() # call self every 1000 ms (1 sec) self.clock.after(1000, self.increnment_time) if __name__ == '__main__': clk = Clock()