Calculator
Calculator
Calculator
# Creating a window
root = tkinter.Tk()
root.title('Calculator')
root.resizable(width=False, height=False)
# Buttons 0-9
button_0 = tkinter.Button(root, text="0", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(0))
button_1 = tkinter.Button(root, text="1", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(1))
button_2 = tkinter.Button(root, text="2", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(2))
button_3 = tkinter.Button(root, text="3", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(3))
button_4 = tkinter.Button(root, text="4", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(4))
button_5 = tkinter.Button(root, text="5", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(5))
button_6 = tkinter.Button(root, text="6", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(6))
button_7 = tkinter.Button(root, text="7", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(7))
button_8 = tkinter.Button(root, text="8", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(8))
button_9 = tkinter.Button(root, text="9", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_click(9))
# A Decimial button
button_decimal = tkinter.Button(
root, text=".", padx=30, pady=20, font=('Arial', 10, 'bold'), command=lambda:
button_decimal())
def button_click(number):
current = enter.get()
enter.delete(0, tkinter.END)
enter.insert(0, str(current) + str(number))
def button_clear():
enter.delete(0, tkinter.END)
def button_decimal():
current = enter.get()
if "." not in current:
enter.delete(0, tkinter.END)
enter.insert(0, str(current) + ".")
def button_equal():
second_number = float(enter.get())
enter.delete(0, tkinter.END)
if math_operation == "+":
enter.insert(0, first_number + second_number)
elif math_operation == "-":
enter.insert(0, first_number - second_number)
elif math_operation == "*":
enter.insert(0, first_number * second_number)
elif math_operation == "/":
enter.insert(0, first_number / second_number)