Задача Задача №1114894
А
А
Конечно! Вот пример простого кода для создания калькулятора на Python с использованием библиотеки tkinter:
import tkinter as tk
def click(event):
text = event.widget.cget("text")
if text == "=":
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
elif text == "C":
entry.delete(0, tk.END)
else:
entry.insert(tk.END, text)
root = tk.Tk()
root.geometry("400x500")
root.title("Калькулятор")
entry = tk.Entry(root, font=("Arial", 20))
entry.pack(fill=tk.BOTH, expand=True)
button_frame = tk.Frame(root)
button_frame.pack()
buttons = [
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"C", "0", "=", "+"
]
row = 1
col = 0
for button_text in buttons:
button = tk.Button(button_frame, text=button_text, font=("Arial", 15), width=5, height=2)
button.grid(row=row, column=col, padx=5, pady=5)
button.bind("<Button-1>", click)
col += 1
if col > 3:
col = 0
row += 1
root.mainloop()
Этот код создает простой калькулятор с основными арифметическими операциями с помощью библиотеки tkinter. Вы можете запустить его в своей среде разработки Python для использования калькулятора.
