rss logo

Graphical Clock Application with Python

Python logo

I work on a openbox window manager environment and I wanted to have a window which show me current time (in order to avoid extra hours) on my screen. So I developed a small python program to get it.

Configuration

  • OS : Arch Linux
  • python : 3.7.2
  • tk : 8.6.9-2

Instructions

  • We need to install tk windowing toolkit :
root@host:~# pacman -S extra/tk

Code

#! /usr/bin/python # Role : Graphical Window to show current time # Author : http://shebangthedolphins.net/ # 1.0 first version #links that helped me : # - https://tkdocs.com/tutorial/firstexample.html # - https://stackoverflow.com/questions/44085554/how-to-use-the-after-method-to-make-a-callback-run-periodically # - https://stackoverflow.com/questions/46279096/tkinter-update-variable-real-time # - https://sebsauvage.net/python/gui/ #from tkinter import * from tkinter import ttk import datetime import tkinter as tk class WhatTime(tk.Tk): def __init__(self,parent): tk.Tk.__init__(self,parent) self.parent = parent self.initialize() def initialize(self): try: self.grid() self.resizable(False,False) #I don't want that the window can be resized self.hour = tk.StringVar() #Set hour variable self.wm_attributes("-topmost", 1) #Put window on top of others self.PrintTime() label = tk.Label(self,textvariable=self.hour,anchor="w",fg="green",bg="black") #set graphical theme label.config(font=("Courier", 20, "bold")) #set font label.grid(column=0,row=0,columnspan=1,sticky='EW') except ValueError: pass def PrintTime(self): try: value = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') #set the time output self.hour.set(value) self.after(1000, self.PrintTime) #will be refreshed each second except ValueError: pass if __name__ == "__main__": app = WhatTime(None) app.title('WT2I?') app.mainloop()

Picture

clock image of the python GUI program
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address