Advertisement
Blogdemaths

Blogdemaths - Horloge de Berlin

Sep 14th, 2014
1,560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. ###########################
  2. # blogdemaths.wordpress.com
  3. ###########################
  4.  
  5. ###########################
  6. # Horloge de Berlin
  7. #
  8. # https://blogdemaths.wordpress.com/2014/09/14/lhorloge-de-berlin/
  9. ###########################
  10.  
  11. from tkinter import *
  12. import time
  13.  
  14. def calcul_horloge_Berlin(heure,minute):
  15.     # A partir du temps h;mn renvoie 4 nombres tels que:
  16.     # h = 5a + b et m = 5c +d
  17.     a = h//5
  18.     b = h%5
  19.     c = minute//5
  20.     d = minute%5
  21.    
  22.     return a,b,c,d
  23.  
  24.  
  25. def affichage_rectangles(a,b,c,d):
  26.     marge_gauche = 40
  27.     marge_haut = 50
  28.     #dimensions des rectangles pour les lignes 1, 2 et 4
  29.     longueur_rectangle = 80
  30.     hauteur_rectangle = 50
  31.  
  32.     #dimensions des rectangles de la troisième ligne
  33.     longueur_rectangle_bis = 4*longueur_rectangle / 11
  34.    
  35.    
  36.     for k in range(4):
  37.         if k<=a -1:
  38.             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")
  39.         else:
  40.             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")
  41.     for j in range(4):
  42.         if j <=b -1:
  43.             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")
  44.         else:
  45.             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")
  46.            
  47.     for l in range(11):
  48.         if l <=c -1:
  49.             if l== 2 or l==5 or l==8:
  50.                 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")
  51.             else:
  52.                 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")
  53.         else:
  54.             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")
  55.            
  56.     for i in range(4):
  57.         if i<=d-1:
  58.             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")
  59.         else:
  60.             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")
  61.  
  62. def effacer():
  63.     canvas.delete(ALL)
  64.  
  65. def boucle():
  66.     effacer() #Efface le canvas
  67.     h,m,s= time.localtime()[3:6] #recupere l'heure, les minutes et les secondes actuelles
  68.     heure.set( "{0}h {1}mn {2}sec".format(h,m,s))
  69.  
  70.     a,b,c,d = calcul_horloge_Berlin(h,m)
  71.     affichage_rectangles(a,b,c,d)
  72.    
  73.     affichage_heure.config(textvariable=heure) # mise à jour de l'heure dans le label
  74.     fenetre.update_idletasks()
  75.     #print('tic !')
  76.     fenetre.after(1000,boucle) #ne pas utiliser time.sleep avec tkinter
  77.  
  78.  
  79. #Fenêtre principale            
  80. fenetre = Tk()
  81. fenetre.title("Horloge de Berlin")
  82. fenetre['bg']='dark gray'
  83. Label(fenetre,text="Horloge de Berlin - blogdemaths.wordpress.com", bg= "dark gray").grid(sticky=(N,S,W,E))
  84.  
  85. #Cadre principal
  86. cadre = Frame(fenetre,bg='gray')
  87. cadre.grid(sticky=(N,S,E,W), padx=(10,10),pady=10)
  88.  
  89. #Canvas incluant l'horloge de Berlin
  90. canvas = Canvas(cadre,bg='gray',width=400,height=300)
  91. canvas.grid(column=1,row=1,sticky='N')
  92.  
  93. heure = StringVar()
  94. h,m,s= time.localtime()[3:6] #recupere l'heure, les minutes et les secondes actuelles
  95. heure.set( "{0}h {1}mn {2}sec".format(h,m,s))
  96.  
  97. #Affichage de l'heure "normale"
  98. affichage_heure = Label(cadre,textvariable=heure,fg="black",bg="gray")
  99. affichage_heure.grid(column=2,row=1)
  100.  
  101. boucle()
  102. fenetre.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement