On se propose dans ce mini projet graphique en Python Tkinter de créer une simple calculatrice comme le montre la figure ci-dessous:
- L'interface graphique sera basée sur la disposition géométrique grid (les boutons seront nommés btn_0, btn_1, btn_3, ... , btn_9.
- Le champ de saisi Entry sera nomé equation.
- Nous allons ensuite créer une variable globale qui sera nommée formule qui sera affectée au champ de saisi equation.
- Nous créons ensuite une méthode nommée click() qui permettra de saisir l'etiquette du bouton sur lequel le click a été effectué.
- Nous créons ensuite une méthode nommée equalclick() qui va évaluer l'expression saisie et afficher le résultat sur le champ equation.
- Finalement une méthode effacer() pour effacer le résultat.
Code de l'application calculatrice
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
from tkinter import * formule = "" def click(num): global formule formule = formule + str(num) 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") master.geometry("375x315") 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) minus = Button(master, text=' - ', command=lambda: click("-"), height=2, width=10) minus.grid(row=3, column=3) multiply = Button(master, text=' * ', command=lambda: click("*"), height=2, width=10) multiply.grid(row=4, column=3) divide = Button(master, text=' / ', command=lambda: click("/"), height=2, width=10) divide.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') Decimal= Button(master, text='.', command=lambda: click('.'), height=2, width=10) Decimal.grid(row=5, column=1) percent= Button(master, text='%', command=lambda: click('%'), height=2, width=10) percent.grid(row=6, column=1) inverse= Button(master, text='1/X', height=2, width=10) inverse.grid(row=6, column=2) memo= Button(master, text='M', height=2, width=10) memo.grid(row=6, column=3) master.mainloop() |
Younes Derfoufi
CRMEF OUJDA