bollet.c
9 months ago
2 changed files with 128 additions and 2 deletions
@ -0,0 +1,126 @@ |
|||||
|
import tkinter as tk |
||||
|
from Pile import Pile_chaine as Pile |
||||
|
from math import * |
||||
|
|
||||
|
class ValeurSaisie: |
||||
|
"""classe d'une valeur saise par l'utilisateur""" |
||||
|
def __init__(self, valeur, master): |
||||
|
self.valeur = valeur |
||||
|
|
||||
|
self.label_valeur = tk.Label(master, text=valeur) |
||||
|
self.label_valeur.pack() |
||||
|
|
||||
|
def __del__(self): |
||||
|
"""suppression du label""" |
||||
|
self.label_valeur.destroy() |
||||
|
|
||||
|
class Interface(tk.Frame): |
||||
|
"""interface de la calculatrice""" |
||||
|
def __init__(self, master): |
||||
|
self.master = master |
||||
|
tk.Frame.__init__(self, master) |
||||
|
|
||||
|
self.stack = Pile() |
||||
|
|
||||
|
self.saisie_expression = tk.Entry(self.master) |
||||
|
self.saisie_expression.pack() |
||||
|
self.saisie_expression.bind("<Return>", lambda e : self.entrer()) |
||||
|
self.saisie_expression.bind("<KP_Enter>", lambda e : self.entrer()) |
||||
|
|
||||
|
self.saisie_expression.bind("+", lambda e : self.evaluer("+",2)) |
||||
|
self.saisie_expression.bind("-", lambda e : self.evaluer("-",2)) |
||||
|
self.saisie_expression.bind("*", lambda e : self.evaluer("*",2)) |
||||
|
self.saisie_expression.bind("/", lambda e : self.evaluer("/",2)) |
||||
|
|
||||
|
commandes = tk.LabelFrame(self.master, text="commandes", padx=20, pady=20) |
||||
|
commandes.pack(fill="both") |
||||
|
|
||||
|
btn_calculer = tk.Button(commandes, text='évaluer', command=self.entrer) |
||||
|
btn_calculer.pack(side="left") |
||||
|
|
||||
|
btn_effacer = tk.Button(commandes, text='effacer', command=self.effacer) |
||||
|
btn_effacer.pack(side="left") |
||||
|
|
||||
|
btn_sinus = tk.Button(commandes, text='sin', command=lambda : self.evaluer("sin",1)) |
||||
|
btn_sinus.pack(side="left") |
||||
|
btn_sinus = tk.Button(commandes, text='cos', command=lambda : self.evaluer("cos",1)) |
||||
|
btn_sinus.pack(side="left") |
||||
|
btn_sinus = tk.Button(commandes, text='tan', command=lambda : self.evaluer("tan",1)) |
||||
|
btn_sinus.pack(side="left") |
||||
|
btn_pow = tk.Button(commandes, text='^', command=lambda : self.evaluer("^",2)) |
||||
|
btn_pow.pack(side="right") |
||||
|
|
||||
|
self.affichage_expression = tk.Label(self.master) |
||||
|
self.affichage_expression.pack() |
||||
|
|
||||
|
self.frame_stack = tk.Frame(self.master, bg="gray") |
||||
|
self.frame_stack.pack(padx=10, pady=10) |
||||
|
|
||||
|
self.error_log = tk.Label(self.master, fg="red") |
||||
|
self.error_log.pack() |
||||
|
|
||||
|
def evaluer(self, op, nb_arg): |
||||
|
"""application de l'operation aux nb_arg dernières valeurs du stack""" |
||||
|
self.entrer() |
||||
|
|
||||
|
if nb_arg == 1: |
||||
|
val = self.stack.depiler().valeur |
||||
|
|
||||
|
if op == "sin": |
||||
|
res = sin(val) |
||||
|
if op == "cos": |
||||
|
res = cos(val) |
||||
|
if op == "tan": |
||||
|
res = tan(val) |
||||
|
|
||||
|
elif nb_arg == 2: |
||||
|
try: |
||||
|
droite = self.stack.depiler().valeur |
||||
|
gauche = self.stack.depiler().valeur |
||||
|
except IndexError: |
||||
|
self.error_log.config(text="Il manque un argument") |
||||
|
|
||||
|
if op == "+": |
||||
|
res = gauche + droite |
||||
|
if op == "-": |
||||
|
res = gauche - droite |
||||
|
if op == "*": |
||||
|
res = gauche * droite |
||||
|
if op == "/": |
||||
|
res = gauche / droite |
||||
|
if op == "^": |
||||
|
res = gauche ** droite |
||||
|
|
||||
|
self.stack.empiler(ValeurSaisie(res, self.frame_stack)) |
||||
|
|
||||
|
#https://stackoverflow.com/questions/11541262/basic-query-regarding-bindtags-in-tkinter/11542200#11542200 |
||||
|
return "break" |
||||
|
|
||||
|
def entrer(self): |
||||
|
"""ajoute une nouvelle valeur dans le stack |
||||
|
ajoute un label avec cette valeur""" |
||||
|
valeur_a_ajouter = self.saisie_expression.get().replace(" ", "") |
||||
|
if valeur_a_ajouter != "": |
||||
|
if "." in valeur_a_ajouter: |
||||
|
valeur_a_ajouter = float(valeur_a_ajouter) |
||||
|
else: |
||||
|
valeur_a_ajouter = int(valeur_a_ajouter) |
||||
|
|
||||
|
self.stack.empiler(ValeurSaisie(valeur_a_ajouter, self.frame_stack)) |
||||
|
|
||||
|
self.saisie_expression.delete(0 ,'end') |
||||
|
|
||||
|
def effacer(self): |
||||
|
"""supprime tout le contenu de la pile et les labels associés""" |
||||
|
while not self.stack.est_vide(): |
||||
|
self.stack.depiler() |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
root = tk.Tk() |
||||
|
root.title("Calculatrice") |
||||
|
root.geometry("350x500") |
||||
|
hello_frame = Interface(root) |
||||
|
hello_frame.mainloop() |
Loading…
Reference in new issue