#Калькулятор
import tkinter as tk
from tkinter import ttk
import math
root = tk.Tk()
root.title("Casio Fx123")
root.geometry("400x300")

math_num_1=0
math_num_2=0
result = math_num_1
operation = "..."
print(math_num_1)
print(operation)

def sqrt_root():
    try:
        math_num_1 = float(display.get())
        result = math.sqrt(math_num_1)
        display.delete(0, tk.END)
        display.insert(0, result)
    except ValueError:
        display.delete(0, tk.END)
        display.insert(0, "Error  ")

sum_butt = ttk.Button(text="+",command=lambda: math_operation('+'))
dif_butt =ttk.Button(text="−",command=lambda: math_operation('−'))
multiply_butt =ttk.Button(text="×",command=lambda: math_operation('×'))
div_butt =ttk.Button(text="/",command=lambda: math_operation('/'))

step_butt =ttk.Button(text="X^n",command=lambda: math_operation('X^n'))
root_butt =ttk.Button(text="✓",command=sqrt_root)
percent_butt =ttk.Button(text="%",command=lambda: math_operation('%'))

display = ttk.Entry()

root.columnconfigure(index=0, weight=0)
root.rowconfigure(index=0, weight=0)


def button_click(number):
    current = display.get()
    display.delete(0, tk.END)
    display.insert(0, current + str(number))

def float_dot():
    current = display.get()
    display.delete(0, tk.END)
    display.insert(0, current + '.')

dot_butt = ttk.Button(text=".",command=float_dot)

def clear_all():
    global math_num_1
    global math_num_2
    global operation
    global result
    math_num_1 = 0
    math_num_2 = 0
    result = math_num_1
    operation = "..."
    display.delete(0, tk.END)
    display.insert(0, '')

c_button = ttk.Button(text="C",command=clear_all) # создаем кнопку из пакета ttk

def math_operation(operand):
    global math_num_1
    global operation
    math_num_1 = display.get()
    operation = str(operand)
    display.delete(0, tk.END)
    display.insert(0, "")




def button_equal():
    global math_num_1
    global math_num_2
    global operation
    global result

    try:
        math_num_2 = float(display.get())

        if operation == "+":
            result = float(math_num_1) + math_num_2
            display.delete(0, tk.END)
            display.insert(0, result)
            print(result)
        elif operation == "−":
            result = float(math_num_1) - math_num_2
            display.delete(0, tk.END)
            display.insert(0, result)
            print(result)
        elif operation == "×":
            result = float(math_num_1) * math_num_2
            display.delete(0, tk.END)
            display.insert(0, result)
            print(result)
        elif operation == "×":
            result = float(math_num_1) * math_num_2
            display.delete(0, tk.END)
            display.insert(0, result)
            print(result)
        elif operation == "/":
            if (math_num_2) == 0:
                display.delete(0, tk.END)
                display.insert(0, "Error")
            else:

                result = float(math_num_1) / math_num_2
                display.delete(0, tk.END)
                display.insert(0, result)
                print(result)
        elif operation == "X^n":
            result = float(math_num_1) ** math_num_2
            display.delete(0, tk.END)
            display.insert(0, result)

        elif operation == "%":
            result = ((float(math_num_1) * math_num_2)/100)
            display.delete(0, tk.END)
            display.insert(0, result)

    except ValueError:
        display.delete(0, tk.END)
        display.insert(0, "Error")


eque_butt =ttk.Button(text="=",command= button_equal)


num_1= ttk.Button(text="1",command=lambda: button_click(1)) # создаем кнопку из пакета ttk

num_2= ttk.Button(text="2",command=lambda: button_click(2)) # создаем кнопку из пакета ttk

num_3= ttk.Button(text="3",command=lambda: button_click(3)) # создаем кнопку из пакета ttk

num_4= ttk.Button(text="4",command=lambda: button_click(4)) # создаем кнопку из пакета ttk

num_5= ttk.Button(text="5",command=lambda: button_click(5)) # создаем кнопку из пакета ttk

num_6= ttk.Button(text="6",command=lambda: button_click(6)) # создаем кнопку из пакета ttk

num_7= ttk.Button(text="7",command=lambda: button_click(7)) # создаем кнопку из пакета ttk

num_8= ttk.Button(text="8",command=lambda: button_click(8)) # создаем кнопку из пакета ttk

num_9= ttk.Button(text="9",command=lambda: button_click(9)) # создаем кнопку из пакета ttk

num_0= ttk.Button(text="0",command=lambda: button_click(0)) # создаем кнопку из пакета ttk


display.grid(column=0,row=0,columnspan=5)

c_button.grid(column=0,row=5)    # размещаем кнопку в окн
sum_butt.grid(column=0,row=1)
dif_butt.grid(column=1,row=1)
multiply_butt.grid(column=2,row=1)
div_butt.grid(column=5,row=1)
step_butt.grid(column=5,row=2)
root_butt.grid(column=5,row=3)
percent_butt.grid(column=5,row=4)
eque_butt.grid(column=5,row=5)

num_1.grid(column=0,row=4)
num_2.grid(column=1,row=4)
num_3.grid(column=2,row=4)
num_4.grid(column=0,row=3)
num_5.grid(column=1,row=3)
num_6.grid(column=2,row=3)
num_7.grid(column=0,row=2)
num_8.grid(column=1,row=2)
num_9.grid(column=2,row=2)
num_0.grid(column=1,row=5)
dot_butt.grid(column=2,row=5)


root.mainloop()