Advertisement
AceScottie

tkclock.py

Sep 2nd, 2019
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. from Tkinter import *
  2. from time import strftime
  3.  
  4. class Clock: ##Tkinter is best used as a class object. Makes life easier.
  5.     def __init__(self):
  6.         self.root = Tk()
  7.         self.root.title("Clock")
  8.         self.root.protocol("WM_DELETE_WINDOW", self.quitting) ## handle my own exit code.
  9.         ##set position to be center of screen
  10.         window_width = 1100
  11.         window_height = 400
  12.         ws = self.root.winfo_screenwidth() # width of the screen
  13.         hs = self.root.winfo_screenheight() # height of the screen
  14.         x = (ws/2)-(window_width/2)
  15.         y = (hs/2)-(window_height/2)
  16.         self.root.geometry("%sx%s+%s+%s" %(window_width, window_height, int(x), int(y)))
  17.         ##
  18.         # set window to pop up above others
  19.         self.root.attributes("-topmost", True)
  20.         self.root.attributes("-topmost", False)
  21.         ##
  22.         self.quit = False
  23.         self.curtime = ""
  24.     def update_time(self):
  25.         self.curtime = strftime("%I:%M:%S")
  26.     def run(self):
  27.         display = Label(self.root, font="arial 200 bold", bg="blue violet", fg="lime green")
  28.         display.pack(fill=BOTH, expand=1) # personal preference, i dont like grid()
  29.         while not self.quit:
  30.             self.update_time()
  31.             display.configure(text=self.curtime)
  32.             self.root.update()
  33.             self.root.update_idletasks()
  34.     def quitting(self):
  35.         print("I clicked the X button. i can quit any running processes here.")
  36.         self.quit=True
  37.        
  38. if __name__ == "__main__":
  39.     try:
  40.         app = Clock()
  41.         app.run()
  42.     except Exception as err:
  43.         print(err)
  44.         print("quitting")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement