Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################
- # blogdemaths.wordpress.com
- ###########################
- ###########################
- # Horloge de Berlin
- #
- # https://blogdemaths.wordpress.com/2014/09/14/lhorloge-de-berlin/
- ###########################
- from tkinter import *
- import time
- def calcul_horloge_Berlin(heure,minute):
- # A partir du temps h;mn renvoie 4 nombres tels que:
- # h = 5a + b et m = 5c +d
- a = h//5
- b = h%5
- c = minute//5
- d = minute%5
- return a,b,c,d
- def affichage_rectangles(a,b,c,d):
- marge_gauche = 40
- marge_haut = 50
- #dimensions des rectangles pour les lignes 1, 2 et 4
- longueur_rectangle = 80
- hauteur_rectangle = 50
- #dimensions des rectangles de la troisième ligne
- longueur_rectangle_bis = 4*longueur_rectangle / 11
- for k in range(4):
- if k<=a -1:
- canvas.create_rectangle(marge_gauche + k*longueur_rectangle + 0*hauteur_rectangle, marge_haut , marge_gauche + (k+1)*longueur_rectangle , marge_haut + hauteur_rectangle ,fill="yellow")
- else:
- canvas.create_rectangle(marge_gauche + k*longueur_rectangle + 0*hauteur_rectangle , marge_haut , marge_gauche + (k+1)*longueur_rectangle , marge_haut + hauteur_rectangle ,fill="white")
- for j in range(4):
- if j <=b -1:
- canvas.create_rectangle(marge_gauche + j*longueur_rectangle , marge_haut + 1*hauteur_rectangle, marge_gauche + (j+1)*longueur_rectangle , marge_haut + 2*hauteur_rectangle,fill="yellow")
- else:
- canvas.create_rectangle(marge_gauche + j*longueur_rectangle, marge_haut + 1*hauteur_rectangle , marge_gauche + (j+1)*longueur_rectangle, marge_haut + 2*hauteur_rectangle,fill="white")
- for l in range(11):
- if l <=c -1:
- if l== 2 or l==5 or l==8:
- canvas.create_rectangle( marge_gauche +l*longueur_rectangle_bis, marge_haut + 2*hauteur_rectangle , marge_gauche + (l+1)*longueur_rectangle_bis , marge_haut + 3*hauteur_rectangle,fill="red")
- else:
- canvas.create_rectangle( marge_gauche +l*longueur_rectangle_bis, marge_haut + 2*hauteur_rectangle , marge_gauche + (l+1)*longueur_rectangle_bis , marge_haut + 3*hauteur_rectangle, fill="yellow")
- else:
- canvas.create_rectangle( marge_gauche +l*longueur_rectangle_bis, marge_haut + 2*hauteur_rectangle , marge_gauche + (l+1)*longueur_rectangle_bis, marge_haut + 3*hauteur_rectangle ,fill="white")
- for i in range(4):
- if i<=d-1:
- canvas.create_rectangle( marge_gauche + i*longueur_rectangle, marge_haut + 3*hauteur_rectangle, marge_gauche + (i+1)*longueur_rectangle , marge_haut + 4*hauteur_rectangle ,fill="yellow")
- else:
- canvas.create_rectangle( marge_gauche + i*longueur_rectangle, marge_haut + 3*hauteur_rectangle , marge_gauche + (i+1)*longueur_rectangle , marge_haut + 4*hauteur_rectangle ,fill="white")
- def effacer():
- canvas.delete(ALL)
- def boucle():
- effacer() #Efface le canvas
- h,m,s= time.localtime()[3:6] #recupere l'heure, les minutes et les secondes actuelles
- heure.set( "{0}h {1}mn {2}sec".format(h,m,s))
- a,b,c,d = calcul_horloge_Berlin(h,m)
- affichage_rectangles(a,b,c,d)
- affichage_heure.config(textvariable=heure) # mise à jour de l'heure dans le label
- fenetre.update_idletasks()
- #print('tic !')
- fenetre.after(1000,boucle) #ne pas utiliser time.sleep avec tkinter
- #Fenêtre principale
- fenetre = Tk()
- fenetre.title("Horloge de Berlin")
- fenetre['bg']='dark gray'
- Label(fenetre,text="Horloge de Berlin - blogdemaths.wordpress.com", bg= "dark gray").grid(sticky=(N,S,W,E))
- #Cadre principal
- cadre = Frame(fenetre,bg='gray')
- cadre.grid(sticky=(N,S,E,W), padx=(10,10),pady=10)
- #Canvas incluant l'horloge de Berlin
- canvas = Canvas(cadre,bg='gray',width=400,height=300)
- canvas.grid(column=1,row=1,sticky='N')
- heure = StringVar()
- h,m,s= time.localtime()[3:6] #recupere l'heure, les minutes et les secondes actuelles
- heure.set( "{0}h {1}mn {2}sec".format(h,m,s))
- #Affichage de l'heure "normale"
- affichage_heure = Label(cadre,textvariable=heure,fg="black",bg="gray")
- affichage_heure.grid(column=2,row=1)
- boucle()
- fenetre.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement