Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # -*- coding: utf-8 -*-
- # Copyright © 2016 Ordinosor <https://twitter.com/Ordinosor>
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>
- from tkinter import *
- from math import *
- # Classes :
- class Clavier(object) :
- "Instanciation des touches du clavier"
- def __init__(self, cadre, affichage, signe, commande, ligne, colonne, largeur = 6, hauteur = 4, bordure = 1, relief='raised', rospan = 1, cospan = 1, bg = '#CECECE') :
- "Méthode constructeur"
- self.cadre = cadre
- self.affichage = affichage
- self.signe = signe # attributs d'instance
- self.commande = commande
- self.ligne = ligne
- self.colonne = colonne
- self.largeur = largeur
- self.hauteur = hauteur
- self.bordure = bordure
- self.relief = relief
- self.rospan = rospan
- self.cospan = cospan
- self.bg = bg
- def touches(self) :
- "Création des touches"
- 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)
- class Operations(object) :
- "Opérations d'affichage et de calcul"
- i = 0 # Attribut de classe (variable constante définie pour toute la classe)
- resultat = '' # Attribut de classe
- resultat2 = ''
- memoire = ''
- def __init__(self, affichage, touche = '0', nombre = '', nombre1 = '', nombre2 = '') :
- "Méthode constructeur"
- self.affichage = affichage
- self.touche = touche
- self.nombre = nombre # attributs d'instance
- self.nombre1 = nombre1
- self.nombre2 = nombre2
- def operation(self) :
- if self.touche == 'x' :
- self.touche = '*'
- 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') :
- if Operations.resultat != '' :
- self.affichage.delete(1.0, 'end')
- Operations.resultat = ''
- self.affichage.insert(1.0 + (Operations.i/10), self.touche)
- Operations.i += 1
- def plus_moins(self) :
- self.nombre = self.affichage.get(1.0, 'end')
- self.nombre = self.nombre.strip()
- self.nombre = float(self.nombre)
- if self.nombre < 0 :
- self.nombre = abs(self.nombre)
- elif self.nombre > 0 :
- self.nombre *= -1
- else :
- self.nombre = 0
- self.affichage.delete(1.0, 'end')
- self.affichage.insert(1.0 + (Operations.i/10), self.nombre)
- Operations.i += 1
- def calculer(self) :
- "méthode qui calcule le résultat lorsqu'on clique sur '='"
- Operations.resultat = self.affichage.get(1.0, 'end')
- self.affichage.delete(1.0, 'end')
- Operations.resultat = Operations.resultat.strip()
- self.affichage.insert(1.0, str(eval(Operations.resultat)))
- def calculer2(self, event) :
- Operations.resultat = self.affichage.get(1.0, 'end')
- self.affichage.delete(1.0, 'end')
- Operations.resultat = Operations.resultat.strip()
- self.affichage.insert(1.0, str(eval(Operations.resultat)))
- def calcul_pourcentage(self) :
- Operations.resultat = self.affichage.get(1.0, 'end')
- self.affichage.delete(1.0, 'end')
- Operations.resultat = Operations.resultat.strip()
- if '*' in Operations.resultat :
- ind = Operations.resultat.index('*')
- self.nombre1 = Operations.resultat[0:ind]
- self.nombre2 = Operations.resultat[(ind+1):]
- self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
- Operations.resultat = (self.nombre1/100) * self.nombre2
- self.affichage.insert(1.0, Operations.resultat)
- elif '/' in Operations.resultat :
- ind = Operations.resultat.index('/')
- self.nombre1 = Operations.resultat[0:ind]
- self.nombre2 = Operations.resultat[(ind+1):]
- self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
- Operations.resultat = (self.nombre1*100) / self.nombre2
- self.affichage.insert(1.0, Operations.resultat)
- elif '+' in Operations.resultat :
- ind = Operations.resultat.index('+')
- self.nombre1 = Operations.resultat[0:ind]
- self.nombre2 = Operations.resultat[(ind+1):]
- self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
- Operations.resultat = (self.nombre1/100) * (100 + self.nombre2)
- self.affichage.insert(1.0, Operations.resultat)
- elif '-' in Operations.resultat :
- ind = Operations.resultat.index('-')
- self.nombre1 = Operations.resultat[0:ind]
- self.nombre2 = Operations.resultat[(ind+1):]
- self.nombre1, self.nombre2 = int(self.nombre1), int(self.nombre2)
- Operations.resultat = (self.nombre1/100) * (100 - self.nombre2)
- self.affichage.insert(1.0, Operations.resultat)
- def effacer(self) :
- self.affichage.delete(1.0, 'end')
- def effacer_dernier_chiffre(self) :
- Operations.resultat2 = self.affichage.get(1.0, 'end')
- Operations.resultat2 = Operations.resultat2.strip()
- Operations.resultat2 = Operations.resultat2[:-1]
- self.affichage.delete(1.0, 'end')
- self.affichage.insert(1.0, Operations.resultat2)
- def puissance_deux(self) :
- Operations.resultat = self.affichage.get(1.0, 'end')
- self.affichage.delete(1.0, 'end')
- Operations.resultat = Operations.resultat.strip()
- Operations.resultat = float(Operations.resultat)**2
- self.affichage.insert(1.0, Operations.resultat)
- def racine_carre(self) :
- Operations.resultat = self.affichage.get(1.0, 'end')
- Operations.resultat = Operations.resultat.strip()
- Operations.resultat = sqrt(float(Operations.resultat))
- self.affichage.delete(1.0, 'end')
- self.affichage.insert(1.0, Operations.resultat)
- def ajouter_memoire(self) :
- Operations.memoire = self.affichage.get(1.0, 'end')
- self.affichage.delete(1.0, 'end')
- def afficher_memoire(self) :
- self.affichage.insert(1.0 + (Operations.i/10), Operations.memoire)
- def effacer_memoire(self) :
- Operations.memoire = ''
- # ----- Programme principal :
- if __name__ == "__main__":
- calculatrice = Tk()
- calculatrice.title('Calculatrice de Benedikt')
- calculatrice.resizable(True, True)
- affichage = Text(calculatrice, width=11, height=1, bg='white', bd=2, font="sans 40")
- affichage.grid(row=0, column = 0)
- cadre = Frame(calculatrice, relief=RAISED, border = 0, bg='white')
- cadre.grid(row=1, column=0, padx=5, pady=5)
- calcul2 = Operations(affichage)
- cadre.bind_all('<Return>', calcul2.calculer2)
- # Objets créés par instanciation.
- touche = ["(", ")", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "/", "x", "-", "+", ".", "%"]
- 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
- 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
- operation_touche = Operations(affichage, touche[i])
- signe = Clavier(cadre, affichage, touche[i], operation_touche.operation, ligne_colonne[i][0], ligne_colonne[i][1])
- signe.touches()
- quitter = Clavier(cadre, affichage, "QUITTER", calculatrice.destroy, 1, 0, 16, cospan = 2, bg = '#CECECE')
- quitter.touches()
- operation_C = Operations(affichage)
- C = Clavier(cadre, affichage, 'C', operation_C.effacer, 1, 2)
- C.touches()
- operation_CE = Operations(affichage)
- CE = Clavier(cadre, affichage, 'CE', operation_CE.effacer_dernier_chiffre, 1, 3)
- CE.touches()
- operation_x2 = Operations(affichage)
- x2 = Clavier(cadre, affichage, 'x²', operation_x2.puissance_deux, 1, 4)
- x2.touches()
- operation_MC = Operations(affichage)
- MC = Clavier(cadre, affichage, 'MC',operation_MC.effacer_memoire, 2, 2)
- MC.touches()
- operation_MR = Operations(affichage)
- MR = Clavier(cadre, affichage, 'MR', operation_MR.afficher_memoire, 2, 3)
- MR.touches()
- operation_Mplus = Operations(affichage)
- Mplus = Clavier(cadre, affichage, 'M+', operation_Mplus.ajouter_memoire, 2, 4)
- Mplus.touches()
- operation_racine = Operations(affichage)
- racine = Clavier(cadre, affichage, '√', operation_racine.racine_carre, 3, 4)
- racine.touches()
- operation_egal = Operations(affichage)
- egal = Clavier(cadre, affichage, '=', operation_egal.calculer, 5, 4, hauteur = 9, rospan=2)
- egal.touches()
- operation_plusMoins = Operations(affichage)
- plusMoins = Clavier(cadre, affichage, '+/-', operation_plusMoins.plus_moins, 6, 2)
- plusMoins.touches()
- calculatrice.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement