Horloge Graphique avec Python
- Mise à jour le 10 févr. 2024
 
Je travaille avec le gestionnaire de fenètres openbox and je voulais avoir une fenêtre qui pourrait m'indiquer l'heure (pour éviter les heuress supp…). J'ai donc développé un petit programme python pour ça.
Configuration
- OS : Arch Linux
 - python : 3.7.2
 - tk : 8.6.9-2
 
Instructions
- On a beosin d'installer la boite à outils graphique TK :
 
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()
			Capture
- Download : wt2i.py