import tkinter as tk
def add_digit_to_lbl(digit):
if len(operation_label["text"]) > 0 and len(last_num_label["text"]) < 15:
last_num_label_text = last_num_label["text"]
last_num_label.configure(text=f"{last_num_label_text}" + str(digit))
return
elif "=" in str(first_num_label["text"]):
first_num_label["text"] = digit
last_num_label["text"] = ""
operation_label["text"] = ""
return
elif len(first_num_label["text"]) < 15:
first_num_label_text = first_num_label["text"]
first_num_label.configure(text=f"{first_num_label_text}"+str(digit))
return
def operation_symbol(symbol):
operation_label.configure(text=str(symbol))
def clear_func():
if len(operation_label["text"]) > 0:
last_num_label.configure(text="")
return
else:
first_num_label.configure(text="")
def operations():
first_num = int(first_num_label["text"])
last_num = int(last_num_label["text"])
operation = operation_label["text"]
if operation == "+":
result = first_num + last_num
result_text = f"{first_num} + {last_num} = {result}"
if len(result_text) > 32:
result_text = result_text[:32] + "\n" + result_text[32:]
operation_label.configure(text="")
last_num_label.configure(text="")
first_num_label.configure(text=result_text)
elif operation == "-":
result = first_num - last_num
operation_label.configure(text="")
last_num_label.configure(text="")
result_text = f"{first_num} + {last_num} = {result}"
if len(result_text) > 32:
result_text = result_text[:32] + "\n" + result_text[32:]
first_num_label.configure(text=result_text)
elif operation == "*":
result = first_num * last_num
operation_label.configure(text="")
last_num_label.configure(text="")
result_text = f"{first_num} * {last_num} = {result}"
if len(result_text) > 32 and len(result_text) < 64:
result_text = result_text[:32] + "\n" + result_text[32:]
elif len(result_text) > 64:
index = str(result_text).index("=")
result_text = str(result_text[:index-1]) + "\n=" + str(result_text[index+2:index+4]) + "e" + str(len(result_text[index+4:]))
first_num_label.configure(text=result_text)
elif operation == "/":
result = int(first_num / last_num)
operation_label.configure(text="")
last_num_label.configure(text="")
result_text = f"{first_num} + {last_num} = {result}"
if len(result_text) > 32:
result_text = result_text[:32] + "\n" + result_text[32:]
first_num_label.configure(text=result_text)
elif operation == "^":
# command of power operand button
result = int(first_num ** last_num)
# there are 3 lbls, 1st on top for first num, 2nd on middle for operand, 3rd on bottom for last num
# here when user push "=" button, 2nd and 3rd lbls will be cleared, instead of 1st lbl there will be result
operation_label.configure(text="")
last_num_label.configure(text="")
result_text = f"{first_num} ^ {last_num} = {result}"
index = str(result_text).index("=") # this var will be used to adapt nums and symbols
if len(result_text) > 32 and len(result_text) < 64: # lbls width is 33, height is 3, this clause for 2 lined result_text
result_text = result_text[:index] + "\n" + result_text[index:]
elif len(result_text) > 64 and len(result_text) < 96: # this for 3 lined result_text
result_text = str(result_text[:index]) + "\n" + str(result_text[index:index+4]) + "e" + str(len(result_text[index+4:]))
first_num_label.configure(text=str(result_text)) # print result on first lab
window = tk.Tk()
entry_frame = tk.Frame(width=60, height=9, relief=tk.SUNKEN, borderwidth=5)
first_num_label = tk.Label(master=entry_frame, width=33, height=3, anchor="ne")
operation_label = tk.Label(master=entry_frame, width=33, height=3, anchor="ne")
last_num_label = tk.Label(master=entry_frame, width=33, height=2, anchor="ne")
entry_frame.pack()
first_num_label.pack()
operation_label.pack()
last_num_label.pack()
keys_frame = tk.Frame(relief=tk.SUNKEN, borderwidth=5, width=60)
keys_frame.columnconfigure([i for i in range(4)], minsize=20)
keys_frame.rowconfigure([i for i in range(5)], minsize=20)
keys_frame.pack()
clear_btn = tk.Button(text="C", master=keys_frame, width=7,
command=clear_func)
clear_btn.grid(column=0, row=0, ipady=15)
power_btn = tk.Button(text="^", master=keys_frame, width=7, command=lambda: operation_symbol("^"))
power_btn.grid(column=1, row=0, ipady=15)
addition_btn = tk.Button(text="+", master=keys_frame, width=7, command=lambda: operation_symbol("+"))
addition_btn.grid(column=2, row=0, ipady=15)
deduction_btn = tk.Button(text="-", master=keys_frame, width=7, command=lambda: operation_symbol("-"))
deduction_btn.grid(column=3, row=0, ipady=15)
one_btn = tk.Button(text="1", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(1))
one_btn.grid(column=0, row=1, ipady=15)
two_btn = tk.Button(text="2", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(2))
two_btn.grid(column=1, row=1, ipady=15)
three_btn = tk.Button(text="3", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(3))
three_btn.grid(column=2, row=1, ipady=15)
division_btn = tk.Button(text="/", master=keys_frame, width=7, command=lambda: operation_symbol("/"))
division_btn.grid(column=3, row=1, ipady=15)
four_btn = tk.Button(text="4", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(4))
four_btn.grid(column=0, row=2, ipady=15)
five_btn = tk.Button(text="5", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(5))
five_btn.grid(column=1, row=2, ipady=15)
six_btn = tk.Button(text="6", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(6))
six_btn.grid(column=2, row=2, ipady=15)
multiplication_btn = tk.Button(text="*", master=keys_frame, width=7, command=lambda: operation_symbol("*"))
multiplication_btn.grid(column=3, row=2, ipady=15)
seven_btn = tk.Button(text="7", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(7))
seven_btn.grid(column=0, row=3, ipady=15)
eight_btn = tk.Button(text="8", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(8))
eight_btn.grid(column=1, row=3, ipady=15)
nine_btn = tk.Button(text="9", master=keys_frame, width=7,
command=lambda: add_digit_to_lbl(9))
nine_btn.grid(column=2, row=3, ipady=15)
equality_btn = tk.Button(text="=", master=keys_frame, width=7, command=operations)
equality_btn.grid(column=3, row=3, ipady=15)
window.mainloop()