Advertisement
Kaadem-85

calculatrice.POO.py

Feb 19th, 2016
2,660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.04 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  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. from tkinter import *
  19. from math import *
  20.  
  21. # Classes :
  22.  
  23. class Clavier(object) :
  24.     "Instanciation des touches du clavier"
  25.     def __init__(self, cadre, affichage, signe, commande, ligne, colonne, largeur = 6, hauteur = 4, bordure = 1, relief='raised', rospan = 1, cospan = 1, bg = '#CECECE') :
  26.         "Méthode constructeur"
  27.         self.cadre = cadre  
  28.         self.affichage = affichage
  29.         self.signe = signe # attributs d'instance
  30.         self.commande = commande
  31.         self.ligne = ligne
  32.         self.colonne = colonne
  33.         self.largeur = largeur
  34.         self.hauteur = hauteur
  35.         self.bordure = bordure
  36.         self.relief = relief
  37.         self.rospan = rospan
  38.         self.cospan = cospan
  39.         self.bg = bg
  40.  
  41.  
  42.     def touches(self) :
  43.         "Création des touches"
  44.         Button(self.cadre, text = self.signe, width = self.largeur, height = self.hauteur, relief = self.relief, border = self.bordure, command = self.commande, bg = self.bg).grid(row = self.ligne, column = self.colonne, rowspan = self.rospan, columnspan = self.cospan)
  45.  
  46.    
  47. class Operations(object) :
  48.     "Opérations d'affichage et de calcul"
  49.     i = 0 # Attribut de classe (variable constante définie pour toute la classe)
  50.     resultat = '' # Attribut de classe
  51.     resultat2 = ''
  52.     memoire = ''
  53.     def __init__(self, affichage, touche = '0', nombre = '', nombre1 = '', nombre2 = '') :
  54.         "Méthode constructeur"
  55.         self.affichage = affichage
  56.         self.touche = touche
  57.         self.nombre = nombre # attributs d'instance
  58.         self.nombre1 = nombre1
  59.         self.nombre2 = nombre2
  60.        
  61.     def operation(self) :
  62.         if self.touche == 'x' :
  63.             self.touche = '*'
  64.         if self.touche != '*' and self.touche != '/' and self.touche != '+' and  self.touche != '-'  and '*' not in self.affichage.get(1.0, 'end') and '/' not in self.affichage.get(1.0, 'end') and '+' not in self.affichage.get(1.0, 'end') and '-' not in self.affichage.get(1.0, 'end') :
  65.             if Operations.resultat != '' :
  66.                 self.affichage.delete(1.0, 'end')
  67.                 Operations.resultat = ''
  68.         self.affichage.insert(1.0 + (Operations.i/10), self.touche)
  69.         Operations.i += 1
  70.        
  71.     def plus_moins(self) :
  72.         self.nombre = self.affichage.get(1.0, 'end')
  73.         self.nombre = self.nombre.strip()
  74.         self.nombre = float(self.nombre)
  75.         if self.nombre < 0 :
  76.             self.nombre = abs(self.nombre)
  77.         elif self.nombre > 0 :
  78.             self.nombre *= -1
  79.         else :
  80.             self.nombre = 0
  81.         self.affichage.delete(1.0, 'end')
  82.         self.affichage.insert(1.0 + (Operations.i/10), self.nombre)
  83.         Operations.i += 1
  84.  
  85.     def calculer(self) :
  86.         "méthode qui calcule le résultat lorsqu'on clique sur '='"
  87.         Operations.resultat = self.affichage.get(1.0, 'end')
  88.         self.affichage.delete(1.0, 'end')
  89.         Operations.resultat = Operations.resultat.strip()
  90.         self.affichage.insert(1.0, str(eval(Operations.resultat)))
  91.  
  92.     def calculer2(self, event) :
  93.         Operations.resultat = self.affichage.get(1.0, 'end')
  94.         self.affichage.delete(1.0, 'end')
  95.         Operations.resultat = Operations.resultat.strip()
  96.         self.affichage.insert(1.0, str(eval(Operations.resultat)))
  97.  
  98.     def calcul_pourcentage(self) :
  99.         Operations.resultat = self.affichage.get(1.0, 'end')
  100.         self.affichage.delete(1.0, 'end')
  101.         Operations.resultat = Operations.resultat.strip()
  102.         if '*' in Operations.resultat :
  103.             ind = Operations.resultat.index('*')
  104.             self.nombre1 = Operations.resultat[0:ind]
  105.             self.nombre2 = Operations.resultat[(ind+1):]
  106.             self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
  107.             Operations.resultat = (self.nombre1/100) * self.nombre2
  108.             self.affichage.insert(1.0, Operations.resultat)
  109.         elif '/' in Operations.resultat :
  110.             ind = Operations.resultat.index('/')
  111.             self.nombre1 = Operations.resultat[0:ind]
  112.             self.nombre2 = Operations.resultat[(ind+1):]
  113.             self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
  114.             Operations.resultat = (self.nombre1*100) / self.nombre2
  115.             self.affichage.insert(1.0, Operations.resultat)
  116.         elif '+' in Operations.resultat :
  117.             ind = Operations.resultat.index('+')
  118.             self.nombre1 = Operations.resultat[0:ind]
  119.             self.nombre2 = Operations.resultat[(ind+1):]
  120.             self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
  121.             Operations.resultat = (self.nombre1/100) * (100 + self.nombre2)
  122.             self.affichage.insert(1.0, Operations.resultat)
  123.         elif '-' in Operations.resultat :
  124.             ind = Operations.resultat.index('-')
  125.             self.nombre1 = Operations.resultat[0:ind]
  126.             self.nombre2 = Operations.resultat[(ind+1):]
  127.             self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
  128.             Operations.resultat = (self.nombre1/100) * (100 - self.nombre2)
  129.             self.affichage.insert(1.0, Operations.resultat)
  130.  
  131.     def effacer(self) :
  132.         self.affichage.delete(1.0, 'end')
  133.  
  134.     def effacer_dernier_chiffre(self) :
  135.         Operations.resultat2 = self.affichage.get(1.0, 'end')
  136.         Operations.resultat2 = Operations.resultat2.strip()
  137.         Operations.resultat2 = Operations.resultat2[:-1]
  138.         self.affichage.delete(1.0, 'end')
  139.         self.affichage.insert(1.0, Operations.resultat2)
  140.  
  141.     def puissance_deux(self) :
  142.         Operations.resultat = self.affichage.get(1.0, 'end')
  143.         self.affichage.delete(1.0, 'end')
  144.         Operations.resultat = Operations.resultat.strip()
  145.         Operations.resultat = float(Operations.resultat)**2
  146.         self.affichage.insert(1.0, Operations.resultat)
  147.  
  148.     def racine_carre(self) :
  149.         Operations.resultat = self.affichage.get(1.0, 'end')
  150.         Operations.resultat = Operations.resultat.strip()
  151.         Operations.resultat = sqrt(float(Operations.resultat))
  152.         self.affichage.delete(1.0, 'end')
  153.         self.affichage.insert(1.0, Operations.resultat)
  154.  
  155.     def ajouter_memoire(self) :
  156.         Operations.memoire = self.affichage.get(1.0, 'end')
  157.         self.affichage.delete(1.0, 'end')  
  158.  
  159.     def afficher_memoire(self) :
  160.         self.affichage.insert(1.0 + (Operations.i/10), Operations.memoire)
  161.  
  162.     def effacer_memoire(self) :
  163.         Operations.memoire = ''
  164.  
  165.  
  166. # ----- Programme principal :
  167.  
  168. if __name__ == "__main__":
  169.  
  170.     calculatrice = Tk()
  171.     calculatrice.title('Calculatrice de Benedikt')
  172.     calculatrice.resizable(True, True)
  173.     affichage = Text(calculatrice, width=11, height=1, bg='white', bd=2, font="sans 40")
  174.     affichage.grid(row=0, column = 0)
  175.     cadre = Frame(calculatrice, relief=RAISED, border = 0, bg='white')
  176.     cadre.grid(row=1, column=0, padx=5, pady=5)
  177.     calcul2 = Operations(affichage)
  178.     cadre.bind_all('<Return>', calcul2.calculer2)
  179.  
  180.     # Objets créés par instanciation.
  181.    
  182.     touche =  ["(", ")", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "/", "x", "-", "+", ".", "%"]
  183.     ligne_colonne = [[2, 0], [2, 1], [6, 0], [5, 0], [5, 1], [5, 2], [4, 0], [4, 1], [4, 2], [3, 0], [3, 1], [3, 2], [3, 3], [4, 3], [5, 3], [6, 3], [6, 1], [4, 4]] # Disposition des touches sur le clavier
  184.    
  185.     for i in range(0, len(touche)) : # Boucle qui créé une partie des touches en faisant appel à la méthode operation de la classe Operations et à la méthode touches de la classe Clavier
  186.         operation_touche = Operations(affichage, touche[i])
  187.         signe = Clavier(cadre, affichage, touche[i], operation_touche.operation, ligne_colonne[i][0], ligne_colonne[i][1])
  188.         signe.touches()
  189.      
  190.     quitter = Clavier(cadre, affichage, "QUITTER", calculatrice.destroy, 1, 0, 16, cospan = 2, bg = '#CECECE')
  191.     quitter.touches()
  192.  
  193.     operation_C = Operations(affichage)
  194.     C = Clavier(cadre, affichage, 'C', operation_C.effacer, 1, 2)
  195.     C.touches()
  196.  
  197.     operation_CE = Operations(affichage)
  198.     CE = Clavier(cadre, affichage, 'CE', operation_CE.effacer_dernier_chiffre, 1, 3)
  199.     CE.touches()
  200.  
  201.     operation_x2 = Operations(affichage)
  202.     x2 = Clavier(cadre, affichage, 'x²', operation_x2.puissance_deux, 1, 4)
  203.     x2.touches()
  204.  
  205.     operation_MC = Operations(affichage)
  206.     MC = Clavier(cadre, affichage, 'MC',operation_MC.effacer_memoire, 2, 2)
  207.     MC.touches()
  208.  
  209.     operation_MR = Operations(affichage)
  210.     MR = Clavier(cadre, affichage, 'MR', operation_MR.afficher_memoire, 2, 3)
  211.     MR.touches()
  212.  
  213.     operation_Mplus = Operations(affichage)
  214.     Mplus = Clavier(cadre, affichage, 'M+', operation_Mplus.ajouter_memoire, 2, 4)
  215.     Mplus.touches()
  216.  
  217.     operation_racine = Operations(affichage)
  218.     racine = Clavier(cadre, affichage, '√', operation_racine.racine_carre, 3, 4)
  219.     racine.touches()
  220.  
  221.     operation_egal = Operations(affichage)
  222.     egal = Clavier(cadre, affichage, '=', operation_egal.calculer, 5, 4, hauteur = 9, rospan=2)
  223.     egal.touches()
  224.  
  225.     operation_plusMoins = Operations(affichage)
  226.     plusMoins = Clavier(cadre, affichage, '+/-', operation_plusMoins.plus_moins, 6, 2)
  227.     plusMoins.touches()
  228.  
  229.     calculatrice.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement