calculatrice #1

Closed
opened 9 months ago by dasilva.e · 1 comments
Owner
There is no content yet.
dasilva.e locked and limited conversation to collaborators 9 months ago
dasilva.e unlocked this conversation 9 months ago
Poster
Owner

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() 
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()
dasilva.e closed this issue 9 months ago
dasilva.e reopened this issue 9 months ago
dasilva.e closed this issue 9 months ago
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.