Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python clock
#2
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()
Reply


Messages In This Thread
Python clock - by menator01 - May-14-2020, 03:33 PM
RE: Python clock - by Larz60+ - May-14-2020, 05:58 PM
RE: Python clock - by menator01 - May-14-2020, 10:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Alarm Clock GUI tickandatock_ 1 4,099 Nov-10-2019, 02:52 AM
Last Post: Larz60+
  tkinter clock Ondrej 5 4,351 Jun-06-2019, 02:09 PM
Last Post: heiner55
  Clock freezes - wx.python glib problem strongheart 3 4,028 Oct-10-2017, 03:36 AM
Last Post: strongheart

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020