Advertisement
AceScottie

Simple_Clock.py

Oct 8th, 2020
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. from tkinter import *
  2. from datetime import datetime
  3. import time as pytime # TIP: datetime also contains time so this makes it easier if using both.
  4. from threading import Thread
  5. class TKGUI:
  6.     def __init__(self):
  7.         self.root= Tk() ##initilise the main window
  8.         self.quit = False ##defaults the quit flag
  9.         self.time = "00:00:00"
  10.        
  11.     def update_time(self): #this just sets the self.time variable to the current time
  12.         self.time = datetime.now().strftime("%H:%M:%S")
  13.  
  14.     def time_thread(self):  #This will run as a thread
  15.         while not self.quit:
  16.             self.update_time()
  17.             pytime.sleep(0.5) #best to add some form of delay to a thread or it uses too much CPU power to run simple things
  18.  
  19.     def update_time_lable(self):    #updates the lable
  20.         self.time_L.configure(text=self.time)
  21.  
  22.     def run(self): ##main part of the application
  23.         self.root.configure(bg="white") #sets the background to white rather than default gray.
  24.         self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##changes the X (close) Button to run a function instead.
  25.         try:
  26.             self.root.iconbitmap("fav.ico") ##sets the application Icon
  27.         except:
  28.             pass
  29.         self.root.title("MyApp")
  30.         self.root.geometry("800x600")
  31.         #############################################################
  32.         #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\#
  33.         #----------------------Code Goes Here ----------------------#
  34.         #///////////////////////////////////////////////////////////#
  35.         #############################################################
  36.         self.time_L = Label(self.root, text=self.time, bg="white", font=("Courier", 44)) # The label to hold the time
  37.         self.time_L.pack(side=TOP)
  38.        
  39.        
  40.         #############################################################
  41.         t=Thread(target=self.time_thread)
  42.         t.start()
  43.         while not self.quit: ##flag to quit the application
  44.             self.root.update_idletasks() #updates the root. same as root.mainloop() but safer and interruptable
  45.             self.root.update() #same as above. This lest you stop the loop or add things to the loop.
  46.             #add extra functions here if you need them to run with the loop#
  47.             if self.time != self.time_L.cget("text"): #adding this for a condition
  48.                 self.update_time_lable() #run a function in the mainloop
  49.  
  50.     def quitting(self): ##to set the quit flag
  51.         self.quit = True
  52.            
  53. if __name__ == "__main__":
  54.     app = TKGUI() ##creates instance of GUI class
  55.     try:
  56.         app.run()# starts the application
  57.     except KeyboardInterrupt:
  58.         app.quitting() ##safely quits the application when crtl+C is pressed
  59.     except:
  60.         raise #you can change this to be your own error handler if needed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement