import tkinter as tk

# Création de la fenêtre principale
root = tk.Tk()
root.title("Calculatrice")

# Champ d'affichage
affichage = tk.Entry(root, width=20, font=("Arial", 18), borderwidth=2, relief="ridge", justify="right")
affichage.grid(row=0, column=0, columnspan=4, padx=5, pady=5)

# Fonctions simples pour chaque action
def press_1(): affichage.insert(tk.END, "1")
def press_2(): affichage.insert(tk.END, "2")
def press_3(): affichage.insert(tk.END, "3")
def press_4(): affichage.insert(tk.END, "4")
def press_5(): affichage.insert(tk.END, "5")
def press_6(): affichage.insert(tk.END, "6")
def press_7(): affichage.insert(tk.END, "7")
def press_8(): affichage.insert(tk.END, "8")
def press_9(): affichage.insert(tk.END, "9")
def press_0(): affichage.insert(tk.END, "0")
def press_virgule(): affichage.insert(tk.END, ".")
def press_plus(): affichage.insert(tk.END, "+")
def press_moins(): affichage.insert(tk.END, "-")
def press_multiplier(): affichage.insert(tk.END, "*")
def press_diviser(): affichage.insert(tk.END, "/")

def clear_affichage():
    # effacer le contenu du widget affichage du 1er au dernier caractère
    # tk.END est une constante définit dans le module tk
    # elle représente le caractère de fin de chaine de caractère
    affichage.delete(0, tk.END)

# calculer et afficher le résultat de l'opération
def calculer():
    try:
        resultat = eval(affichage.get())
        affichage.delete(0, tk.END)
        affichage.insert(tk.END, str(resultat))
    except Exception:
        affichage.delete(0, tk.END)
        affichage.insert(tk.END, "Erreur")

# Placement des boutons 

# Ligne 1
btn_7 = tk.Button(root, text="7", width=5, height=2, command=press_7)
btn_7.grid(row=1, column=0)

btn_8 = tk.Button(root, text="8", width=5, height=2, command=press_8)
btn_8.grid(row=1, column=1)

btn_9 = tk.Button(root, text="9", width=5, height=2, command=press_9)
btn_9.grid(row=1, column=2)

btn_division = tk.Button(root, text="/", width=5, height=2, command=press_diviser)
btn_division.grid(row=1, column=3)

# Ligne 2
btn_4 = tk.Button(root, text="4", width=5, height=2, command=press_4)
btn_4.grid(row=2, column=0)

btn_5 = tk.Button(root, text="5", width=5, height=2, command=press_5)
btn_5.grid(row=2, column=1)

btn_6 = tk.Button(root, text="6", width=5, height=2, command=press_6)
btn_6.grid(row=2, column=2)

btn_multiplication = tk.Button(root, text="*", width=5, height=2, command=press_multiplier)
btn_multiplication.grid(row=2, column=3)

# Ligne 3
btn_1 = tk.Button(root, text="1", width=5, height=2, command=press_1)
btn_1.grid(row=3, column=0)

btn_2 = tk.Button(root, text="2", width=5, height=2, command=press_2)
btn_2.grid(row=3, column=1)

btn_3 = tk.Button(root, text="3", width=5, height=2, command=press_3)
btn_3.grid(row=3, column=2)

btn_soustraction = tk.Button(root, text="-", width=5, height=2, command=press_moins)
btn_soustraction.grid(row=3, column=3)

# Ligne 4
btn_0 = tk.Button(root, text="0", width=5, height=2, command=press_0)
btn_0.grid(row=4, column=0)

btn_virgule = tk.Button(root, text=",", width=5, height=2, command=press_virgule)
btn_virgule.grid(row=4, column=1)

btn_egal = tk.Button(root, text="=", width=5, height=2, command=calculer)
btn_egal.grid(row=4, column=2)

btn_addition = tk.Button(root, text="+", width=5, height=2, command=press_plus)
btn_addition.grid(row=4, column=3)

# Ligne 5 : bouton C (clear)
btn_clear = tk.Button(root, text="Tout effacer", width=20, height=2, command=clear_affichage)
btn_clear.grid(row=5, column=0, columnspan=4, padx=5, pady=5)

# Lancement de la boucle principale
root.mainloop()
