Professional Documents
Culture Documents
def insert(text):
if text != "=":
En.insert("end", text)
else:
try:
operation = eval(En.get())
En.delete(0, "end")
En.insert(0, "{}".format(operation))
except Exception as e:
En.delete(0, "end")
En.insert(0, "Erreur")
def convert_to_binary():
current_text = En.get()
try:
decimal_value = eval(current_text)
binary_value = bin(decimal_value)[2:] # [2:] to remove the '0b' prefix
from the binary representation
En.delete(0, "end")
En.insert(0, "{} (binaire)".format(binary_value))
except Exception as e:
En.delete(0, "end")
En.insert(0, "Erreur")
def power():
current_text = En.get()
En.delete(0, "end")
En.insert(0, "{}**2".format(current_text))
def exponential():
current_text = En.get()
En.delete(0, "end")
En.insert(0, "math.exp({})".format(current_text))
#def calculate_delta():
# try:
# current_text = En.get()
# a, b, c = [float(x) for x in current_text.split()]
#delta = b**2 - 4*a*c
# En.delete(0, "end")
# En.insert(0, "Δ = {}".format(delta))
#except Exception as e:
# En.delete(0, "end")
# En.insert(0, "Erreur")
def plot_graph():
try:
current_text = En.get()
x = range(-10, 11)
y = [eval(current_text.replace("x", str(i))) for i in x]
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel='x', ylabel='y',
title='Graph')
ax.grid()
except Exception as e:
En.delete(0, "end")
En.insert(0, "Erreur")
root = Tk()
root.title("Calculatrice")
root.geometry("600x800")
root.configure(bg="#F2F2F2")
button("4", 1, 2)
button("5", 2, 2)
button("6", 3, 2)
button("7", 1, 3)
button("8", 2, 3)
button("9", 3, 3)
button("+", 1, 4)
button("-", 2, 4)
button("*", 3, 4)
button("/", 1, 5)
button("0", 2, 5)
button(".", 3, 5)
root.mainloop()