import tkinter as tk
def calculate():
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')
root =
tk.Tk ()
root.title('Simple Calculator')
root.geometry('300x400')
entry = tk.Entry(root, font=('Arial', 24), borderwidth=5, relief='ridge')
entry.pack(pady=20, padx=20, fill='both')
buttons_frame = tk.Frame(root)
buttons_frame.pack()
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', 'C', '=', '+'
]
row_val = 0
col_val = 0
for button in buttons:
if button == '=':
btn = tk.Button(buttons_frame, text=button, font=('Arial', 18), command=calculate)
elif button == 'C':
btn = tk.Button(buttons_frame, text=button, font=('Arial', 18), command=lambda: entry.delete(0, tk.END))
else:
btn = tk.Button(buttons_frame, text=button, font=('Arial', 18), command=lambda b=button: entry.insert(tk.END, b))
btn.grid(row=row_val, column=col_val, sticky='nsew', padx=5, pady=5, ipadx=5, ipady=10)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# Устанавливаем равномерное распределение кнопок
for i in range(4):
buttons_frame.grid_columnconfigure(i, weight=1)
root.mainloop()
from unittest import result
from tkinter.tix import COLUMN # type: ignore
def calculate() :
try:
result =eval(entry.get())
tk.Entry.delete (0,
tk.END)
tk.Entry.insert(tk.END,str(result))
except Exception as e:
tk.Entry.delete(0,tk.END)
tk.Entry.inser(tk.END,'Error')
root = tk.Tk()
root.title('Simple Calculator')
root.geometry('300x400')
entry = tk.Entry(root,font=('Arial', 24,borderwidth=5,relief='ridge')entry.pack(pady=20),padx=20, fill='both')
buttons_frame = tk.Frame(root)
buttons_frame.pack()
buttons = [
'7', '8', '9',
'/',
'4', '5', '6',
'*',
'1', '2', '3',
'-' ,
'0', 'C', '=',
'+'
]
row_val = 0
col_val = 0
for button in buttons: if button == '=':
btn =tk.Button(buttons_frame,text=button,font=('Arial', 18), command=calculate)elif button == 'C':
btn =tk.Button(buttons_frame, text=button, font=('Arial', 18), command=lambda: entry.delete(0, tk.END))
else:btn=tk.Button(buttons_frame, text=button, font=('Arial', 18), command=lambda
b=button:entry.insert(tk.END,b))
btn.grid(row=row_val,COLUMN-col_val,sticky='nsew', padx=5, pady=5, ipadx=5, ipady=10)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
for i in range(4):
buttons_frame.grid_columnconfigure(i,weight=1)
root.mainloop()