Advertisement
Kaadem-85

horloge.py

Sep 23rd, 2016
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf8 -*-
  3.  
  4. # Copyright © 2016 Ordinosor <https://twitter.com/Ordinosor>
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>
  17.  
  18. #==== IMPORTATION DES MODULES =============================================================================
  19.  
  20. from tkinter import* # Importation du module tkinter
  21. import time
  22. from datetime import datetime
  23. import math
  24. import threading
  25.  
  26. #==== CLASSE ==============================================================================================
  27.  
  28. class Horloge(object) :
  29.     "Instanciation de l'objet 'horloge'"
  30.    
  31.     def __init__(self) :
  32.         "Constructeur de la classe Horloge"
  33.  
  34. #----------------------------------------------------------------------------------------------------------    
  35.  
  36.     def horloge(self) :
  37.         "Création de l'horloge"
  38.         self.fenetre = Tk()
  39.         self.fenetre.title('Horloge de Benoît')
  40.         self.can = Canvas(self.fenetre, width = 300, height = 300, bg='white')
  41.         self.can.grid()
  42.         self.can.create_oval(15, 15, 285, 285, outline = 'navy', width = 3) # Création du plateau de l'horloge.
  43.         self.can.create_oval(145, 145, 155, 155, fill = 'black', width = 3) # Création du petit cercle noir au centre de l'horloge.
  44.  
  45.         self.numbers_coords = [[210, 50],[250, 90],[272, 150],[255, 212],[210, 255],[150, 270],[95, 255],[50, 210],[28, 150],[47, 95],[92, 52],[150, 35]]
  46.        
  47.         for i in range(0, len(self.numbers_coords)) : # Placement des chiffres de l'horloge
  48.             self.can.create_text(self.numbers_coords[i][0], self.numbers_coords[i][1], text= str(i+1), font = 'Times 14 bold italic', fill = 'black')
  49.            
  50.         self.hour = self.can.create_line(0, 0, 0, 0, fill = 'black', width = 6) # aiguille des heures
  51.         self.minute = self.can.create_line(0, 0, 0, 0, fill = 'black', width = 6) # aiguille des minutes
  52.         self.second = self.can.create_line(0, 0, 0, 0, fill = 'red', width = 3) # aiguille des secondes
  53.        
  54.         self.move() # Appel de la méthode move() pour faire avancer les aiguilles
  55.         self.fenetre.mainloop() # Déclenchement du réceptionnaire d'événements
  56.  
  57. #----------------------------------------------------------------------------------------------------------
  58.  
  59.     def move(self):
  60.         #Méthode permettant aux aiguilles d'avancer
  61.         self.now = time.localtime() # Méthode qui fournit l'heure locale
  62.         self.can.coords(self.hour, 150, 150, 150 + math.cos(self.now[3]*math.pi/6+self.now[4]*math.pi/360-math.pi/2)*55,150 + math.sin(self.now[3]*math.pi/6+self.now[4]*math.pi/360-math.pi/2)*55)
  63.         self.can.coords(self.minute, 150, 150, 150 + math.cos(self.now[4]*math.pi/30+self.now[5]*math.pi/1800-math.pi/2)*85, 150 + math.sin(self.now[4]*math.pi/30+self.now[5]*math.pi/1800-math.pi/2)*85),
  64.         self.can.coords(self.second, 150, 150, 150 + math.cos(self.now[5]*math.pi/30-math.pi/2)*90, 150 + math.sin(self.now[5]*math.pi/30-math.pi/2)*90)  
  65.         # Récursivité : La méthode move s'appelle elle-même toutes les mille millièmes de secondes (c'est-à-dire toutes les secondes) grâce à la méthode after:
  66.         self.can.after(1000, self.move)
  67.  
  68. ###### MAIN PROGRAMM ######################################################################################
  69.  
  70. if __name__ == "__main__"
  71.  
  72.     horloge = Horloge() # Création de l'objet horloge
  73.     horloge.horloge()   # Appel de la méthode horloge de la classe éponyme.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement