from tkinter import *
expression = ""
# press function to add button press functionality

def press(num):
    global expression
    expression = expression + str(num) # add the number or operator at the end of the expression
    equation.set(expression)

# mequalpress function to evaluate the expression in the entry widget and update it with the result

def equalpress():
    global expression
    total = str(eval(expression))
    equation.set(total)
    expression = ""
# Clear function to clear the entry widget
def clear():
    global expression
    expression = ""
    equation.set("")

# Create the main window
gui = Tk()
gui.title("Simple Calculator") # window title
gui.geometry("270x300") #default dimensions
equation = StringVar()
# Create an entry widget for entering expressions
expression_field = Entry(gui, textvariable=equation,width=30,font=('Arial 11'))
expression_field.grid(columnspan=4)

# Create main buttons
# numbers buttons
button1 = Button(gui, text=' 1 ', fg='whitesmoke', bg='sienna',
command=lambda: press(1), height=3, width=8)
button1.grid(row=2, column=0)

button2 = Button(gui, text=' 2 ', fg='whitesmoke', bg='sienna',
command=lambda: press(2), height=3, width=8)
button2.grid(row=2, column=1)

button3 = Button(gui, text=' 3 ', fg='whitesmoke', bg='sienna',
command=lambda: press(3), height=3, width=8)
button3.grid(row=2, column=2)

button4 = Button(gui, text=' 4 ', fg='whitesmoke', bg='sienna',
command=lambda: press(4), height=3, width=8)
button4.grid(row=3, column=0)

button5 = Button(gui, text=' 5 ', fg='whitesmoke', bg='sienna',
command=lambda: press(5), height=3, width=8)
button5.grid(row=3, column=1)

button6 = Button(gui, text=' 6 ', fg='whitesmoke', bg='sienna',
command=lambda: press(6), height=3, width=8)
button6.grid(row=3, column=2)

button7 = Button(gui, text=' 7 ', fg='whitesmoke', bg='sienna',
command=lambda: press(7), height=3, width=8)
button7.grid(row=4, column=0)

button8 = Button(gui, text=' 8 ', fg='whitesmoke', bg='sienna',
command=lambda: press(8), height=3, width=8)
button8.grid(row=4, column=1)

button9 = Button(gui, text=' 9 ', fg='whitesmoke', bg='sienna',
command=lambda: press(9), height=3, width=8)
button9.grid(row=4, column=2)

button0 = Button(gui, text=' 0 ', fg='whitesmoke', bg='sienna',
command=lambda: press(0), height=3, width=8)
button0.grid(row=5, column=0)

# main operators buttons
plus = Button(gui, text=' + ', fg='black', bg='tan',
command=lambda: press("+"), height=3, width=8)
plus.grid(row=2, column=3)

minus = Button(gui, text=' - ', fg='black', bg='tan',
command=lambda: press("-"), height=3, width=8)
minus.grid(row=3, column=3)

mul = Button(gui, text=' * ', fg='black', bg='tan',
command=lambda: press("*"), height=3, width=8)
mul.grid(row=4, column=3)

div = Button(gui, text=' / ', fg='black', bg='tan',
command=lambda: press("/"), height=3, width=8)
div.grid(row=5, column=3)

mod = Button(gui, text=' % ', fg='black', bg='tan',
command=lambda: press("%"), height=3, width=8)
mod.grid(row=5, column=2)

point = Button(gui, text='.',fg='black',bg='tan',
command=lambda: press("."), height=3, width=8)
point.grid(row=5, column=1)

# equal button
equal = Button(gui, text=' = ', fg='black', bg='tan',
command=equalpress, height=3, width=18)
equal.grid(row=6, column=2,columnspan=3)

# clear button
clr = Button(gui, text='Clear',fg='black',bg='tan',
command=clear, height=3, width=18)
clr.grid(row=6, column=0,columnspan=2)

# Run the application
gui.mainloop()