commit 59648dc09933e54d2bf177306229a2009bcfd6a6 Author: ethan.dasilva Date: Fri Feb 16 15:54:50 2024 +0100 premier commit diff --git a/cal.py b/cal.py new file mode 100644 index 0000000..67b27ee --- /dev/null +++ b/cal.py @@ -0,0 +1,125 @@ +class Expression: + def __init__(self, valeur, gauche=None, droit=None): + self.valeur = valeur + self.gauche = gauche + self.droit = droit + + def evalue(self): + if self.valeur == '+': + return self.gauche.evalue() + self.droit.evalue() + elif self.valeur == '*': + return self.gauche.evalue() * self.droit.evalue() + else: + return int(self.valeur) + + def __str__(self): + if self.valeur in ['+', '*']: + return f"({str(self.gauche)} {self.valeur} {str(self.droit)})" + else: + return str(self.valeur) + + def npi2tree(expression_npi): + pile = [] + for element in expression_npi: + if element in ['+', '*']: + droite = pile.pop() + gauche = pile.pop() + pile.append(Expression(element, gauche, droite)) + else: + pile.append(Expression(element)) + return pile[0] + + +from tkinter import * + +formule = "" + +def click(numero): + + global formule + formule = formule + str(numero) + equation.set(formule) + +def equalclick(): + try: + global formule + + result = str(eval(formule)) + equation.set(result) + formule = result + + except: + equation.set(" error ") + formule = "" + +def effacer(): + global formule + formule = "" + equation.set("") + +if __name__ == "__main__": + master = Tk() + master.title("Calculatrice Ethan Terminale") + master.geometry("733x500") + equation = StringVar() + formule_field = Entry(master, textvariable=equation) + formule_field.grid(columnspan=4, pady= 30 , padx = 20 , ipadx = 100 , ipady = 10) + btn_1 = Button(master, text=' 1 ', command=lambda: click(1), height=2, width=10) + btn_1.grid(row=2, column=0) + + btn_2 = Button(master, text=' 2 ', command=lambda: click(2), height=2, width=10) + btn_2.grid(row=2, column=1) + + btn_3 = Button(master, text=' 3 ', command=lambda: click(3), height=2, width=10) + btn_3.grid(row=2, column=2) + + btn_4 = Button(master, text=' 4 ', command=lambda: click(4), height=2, width=10) + btn_4.grid(row=3, column=0) + + btn_5 = Button(master, text=' 5 ', command=lambda: click(5), height=2, width=10) + btn_5.grid(row=3, column=1) + + btn_6 = Button(master, text=' 6 ', command=lambda: click(6), height=2, width=10) + btn_6.grid(row=3, column=2) + + btn_7 = Button(master, text=' 7 ', command=lambda: click(7), height=2, width=10) + btn_7.grid(row=4, column=0) + + btn_8 = Button(master, text=' 8 ', command=lambda: click(8), height=2, width=10) + btn_8.grid(row=4, column=1) + + btn_9 = Button(master, text=' 9 ', command=lambda: click(9), height=2, width=10) + btn_9.grid(row=4, column=2) + + btn_0 = Button(master, text=' 0 ', command=lambda: click(0), height=2, width=10) + btn_0.grid(row=5, column=0) + + plus = Button(master, text=' + ', command=lambda: click("+"), height=2, width=10) + plus.grid(row=2, column=3) + + moins = Button(master, text=' - ', command=lambda: click("-"), height=2, width=10) + moins.grid(row=3, column=3) + + multiplier = Button(master, text=' * ', command=lambda: click("*"), height=2, width=10) + multiplier.grid(row=4, column=3) + + diviser = Button(master, text=' / ', command=lambda: click("/"), height=2, width=10) + diviser.grid(row=5, column=3) + + equal = Button(master, text=' = ', command=equalclick, height=2, width=10) + equal.grid(row=5, column=2) + + effacer = Button(master, text='effacer', command=effacer, height=2, width=10) + effacer.grid(row=6, column='0') + + Decimale= Button(master, text='.', command=lambda: click('.'), height=2, width=10) + Decimale.grid(row=5, column=1) + + pourcent= Button(master, text='%', command=lambda: click('%'), height=2, width=10) + pourcent.grid(row=6, column=1) + + + master.mainloop() + + +