Python : Code calculatrice
Version 1
Code Python libre de droit écrit et offert par benoît de dépannage pc angers. Correction par Johann Leclerc sur Angers.
Ces deux versions de code ont été écrite à la demande d’étudiants.
Programmes écrits pendant notre temps libre.
Si vous avez d’autres versions de ce code à me proposer n’hésitez pas à me contacter. Tél: 06.23.06.09.10
#!/usr/bin/python # -*- coding: utf-8 -*- # --- Import ----------------------------------------------------------- import tkinter as tk import tkinter.ttk as ttk from tkinter.font import Font # --- Classe Calculatrice ---------------------------------------------- class Calculatrice(object): """ Commentaires """ # --- Initialisation de la classe --- def __init__(self, fen): pass self.fen = fen self.txt = "" self.structure() # --- Structure de la page --- def structure(self): #--- Variable design --- bt_bg = "powder blue" bt_font = Font(self.fen, ("arial", 20, "bold")) #--- operator="" self.txt = tk.StringVar() txtDisplay = tk.Entry(self.fen, bd=30, bg=bt_bg, justify='right', font=bt_font, textvariable=self.txt, ) txtDisplay.grid(columnspan=4) #--- Ligne qui peut etre mise a la suite mais pour la lisibilité c'est mieux #--- Liste des elements de la calculatrice liste = ["9","8","7","+","6","5","4","-","3","2","1","*","C","0","=","/"] #--- Déplacement dans le grid pour placement x=1 y=0 #--- Boucle pour placer les différents objets for elem in liste: temp = tk.Button(self.fen, padx=16, bd=8, fg="black", bg=bt_bg, font=bt_font, text=elem, command=lambda elem=elem:self.btnClick(elem)) # ATTENTION : lambda ELEM=ELEM : la fonction (precise a quoi equivaut elem, ca fonctionnait mais prend l'habitude au cas ou ;o) temp.grid(row=x, column=y) y = y + 1 if y>3: y=0 x = x+1 pass #--- Clique sur un bouton --- def btnClick(self, value): ''' Commentaire ''' if value == "=": self.btnEqualsInput() return if value == "C": self.txt.set("") return operator=self.txt.get() + str(value) self.txt.set(operator) #--- Envoi le résultat --- def btnEqualsInput(self): ''' Commentaire ''' sumup=str(eval(self.txt.get())) self.txt.set(sumup) operator="" # --- Fenetre ---------------------------------------------------------- def main(): #--- Creation de la fenetre app_win = tk.Tk() app_win.title("Calculatrice v1.0") #--- Declaration de la classe calc = Calculatrice(app_win) #--- app_win.mainloop() #--- if __name__ == '__main__': main()
Version 2 du code de la calculatrice en python.
#--- Import from tkinter import* from tkinter import font #--- Clique sur un bouton def btnClick(value): ''' Commentaire ''' if value == "=": btnEqualsInput() return if value == "C": text_Input.set("") return global operator operator=operator + str(value) text_Input.set(operator) #--- Envoi le résultat def btnEqualsInput(): ''' Commentaire ''' global operator sumup=str(eval(operator)) text_Input.set(sumup) operator="" #--- Creation de la fenetre cal = Tk() cal.title("Calculator") #--- Variable design --- bt_bg = "powder blue" bt_font = font.Font(cal, ("arial", 20, "bold")) #--- operator="" text_Input = StringVar() txtDisplay = Entry(cal, bd=30, bg=bt_bg, justify='right', font=bt_font, textvariable=text_Input, ) txtDisplay.grid(columnspan=4) #--- Ligne qui peut etre mise a la suite mais pour la lisibilité c'est mieux #--- Liste des elements de la calculatrice liste = ["9","8","7","+","6","5","4","-","3","2","1","*","C","0","=","/"] #--- Déplacement dans le grid pour placement x=1 y=0 #--- Boucle pour placer les différents objets for elem in liste: temp = Button(cal, padx=16, bd=8, fg="black", bg=bt_bg, font=bt_font, text=elem, command=lambda elem=elem:btnClick(elem)) # ATTENTION : lambda ELEM=ELEM : la fonction (precise a quoi equivaut elem, ca fonctionnait mais prend l'habitude au cas ou ;o) temp.grid(row=x, column=y) y = y + 1 if y>3: y=0 x = x+1 #--- cal.mainloop()