import tkinter as tk
from tkinter import ttk
class Calculator:
def __init__(self, root):
self.root = root
root.title("Калькулятор")
root.resizable(False, False)
self.entry = ttk.Entry(root, width=20, font=('Arial', 16))
self.entry.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
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),
('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
('C', 5, 0)
]
for (text, row, col) in buttons:
button = ttk.Button(root, text=text, command=lambda t=text: self.button_click(t))
button.grid(row=row, column=col, sticky='nsew', padx=2, pady=2)
def button_click(self, char):
if char == '=':
try:
result = eval(self.entry.get())
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, str(result))
except:
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, "Ошибка")
elif char == 'C':
self.entry.delete(0, tk.END)
else:
self.entry.insert(tk.END, char)
if __name__ == '__main__':
root = tk.Tk()
calc = Calculator(root)
root.mainloop()