import tkinter as tk
from tkinter import ttk
#
window = tk.Tk()
window.title("Calculator")
window.geometry('280x360')
window.resizable(False,False)
#
def calc(items):
first, op = items[0]
for second, next_op in items[1:]:
if op == '+':
first = summ(first, second)
elif op == '-':
first = sub(first, second)
elif op == '*':
first = mult(first, second)
elif op == '/':
if (second == 0):
first = 'Division by zero is not allowed'
else:
first = div(first, second)
elif op == '%':
first = perc(first, second)
elif op == '**':
first = power(first, second)
op = next_op
return first
#
def summ(first, second):
return first + second
def sub(first, second):
return first - second
def mult(first, second):
return first * second
def div(first, second):
return first / second
def perc(first, second):
return first % second
def power(first, second):
return first ** second
#
items = []
oper = None
result ="testtext"
for c in range(5): window.columnconfigure(index=c, weight=1)
for r in range(7): window.rowconfigure(index=r, weight=1)
def preview():
text = entry.get()
label.config(text=text + " " + oper)
padix = {"ipadx": 4, "ipady": 4, "padx": 4, "pady": 4}
label = ttk.Label(window, text=f"Answer: {result}")
label.grid(row=0, column=0, **padix, columnspan=5, sticky="ne")
new_num = None
def check(new_text, old_text):
global new_num
if new_num:
if len(new_text) > len(old_text):
typed = new_text.replace(old_text, "", 1)
new_num = False
entry.after_idle(lambda: (entry.delete(0, tk.END), entry.insert(0, typed)))
return True
return new_text == "" or new_text.isdigit()
vcmd = (window.register(check), "%P", "%s")
entry = ttk.Entry(window, validate="key", validatecommand=vcmd, justify="right")
entry.grid(row=1, column=0, **padix, columnspan=5, sticky="ew")
ttk.Button(text="⬅").grid(row=2, column=2, **padix, columnspan=3, sticky="ew")
ttk.Button(text="CE").grid(row=2, column=0, **padix, columnspan=1)
ttk.Button(text="C").grid(row=2, column=1, **padix, columnspan=1)
ttk.Button(text="0").grid(row=6, column=0, **padix, columnspan=3, sticky="ew")
ttk.Button(text="=").grid(row=5, column=4, **padix,rowspan=2, sticky="ns")
entry.focus_set()
window.bind("<FocusIn>", lambda e: entry.focus_set())
ops = ["/", "*", "-", "+", "%", "^"]
def set_op(op):
global new_num
if new_num and items:
items[-1][1] = op
else:
items.append([check()])
items.append([number, op])
new_num = True
global oper
oper = val
preview()
def equals():
number = float(entry.get() or 0)
items.append([number, None])
result = calc(items)
entry.delete(0, tk.END)
entry.insert(0, str(result))
items.clear()
for v in range(6):
count = v % 4
col=3
if v > 3:
col = 4
ttk.Button(text=ops[v], command=lambda v=v: set_op(ops[v])).grid(row=3+count, column=col,**padix)
numb = 1
def digit(digit):
global new_num
if new_num:
entry.delete(0, tk.END)
new_num = False
entry.insert(tk.END, digit)
for a in range(3):
for b in range(3):
btn = ttk.Button(text=f"{numb}", command=lambda numb=numb: digit(numb))
btn.grid(row=5-a, column=b, **padix)
numb = numb+1
#
window.mainloop()