code of calculator
1# first install customtkinter . bash : pip install customtkinter
2# code of the calculator . python :
import customtkinter as ctk
app = ctk.CTk()
app.geometry("300x400")
app.title("Calculator")
expression = ""
def add_symbol(symbol):
global expression
expression += str(symbol)
input_var.set(expression)
def clear():
global expression
expression = ""
input_var.set(expression)
def calculate():
global expression
try:
result = eval(expression)
input_var.set(result)
expression = str(result)
except Exception:
input_var.set("Error")
expression = ""
input_var = ctk.StringVar()
display = ctk.CTkEntry(app, textvariable=input_var, font=("Arial", 24), justify="right", width=250, height=50)
display.grid(row=0, column=0, columnspan=4, pady=10, padx=10)
buttons = [
("7", 1, 0), ("8", 1, 1), ("9", 1, 2), ("/", 1, 3),
("4", 2, 0), ("5", 2, 1), ("6", 2, 2), ("*", 2, 3),
("1", 3, 0), ("2", 3, 1), ("3", 3, 2), ("-", 3, 3),
("C", 4, 0), ("0", 4, 1), ("=", 4, 2), ("+", 4, 3),
]
for (text, row, col) in buttons:
if text == "=":
button = ctk.CTkButton(app, text=text, command=calculate, width=50, height=50)
elif text == "C":
button = ctk.CTkButton(app, text=text, command=clear, width=50, height=50)
else:
button = ctk.CTkButton(app, text=text, command=lambda t=text: add_symbol(t), width=50, height=50)
button.grid(row=row, column=col, padx=5, pady=5)
app.mainloop()
Информация по комментариям в разработке