Asd

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

pady=20, font=('Arial', 9, 'bold'), command=lambda:

button_click(9))

# Buttons for mathematical operations


button_add = tkinter.Button(root, text="+", padx=30,
pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("+"))
button_subtract = tkinter.Button(
root, text="-", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("-"))
button_multiply = tkinter.Button(
root, text="*", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("*"))
button_divide = tkinter.Button(
root, text="/", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_operation("/"))
button_equal = tkinter.Button(
root, text="=", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_equal())
button_clear = tkinter.Button(
root, text="C", padx=30, pady=20, font=('Arial', 9, 'bold'), command=lambda:
button_clear())

# A Decimial button
button_decimal = tkinter.Button(
root, text=".", padx=30, pady=20, font=('Arial', 10, 'bold'), command=lambda:
button_decimal())

# Place buttons 0-9 w/ grid


button_0.grid(row=4, column=0)
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

# Place buttons for mathematical operations w/ grid


button_add.grid(row=1, column=3)
button_subtract.grid(row=2, column=3)
button_multiply.grid(row=3, column=3)
button_divide.grid(row=4, column=3)
button_equal.grid(row=4, column=2)
button_clear.grid(row=0, column=3)

# Place a decimal button


button_decimal.grid(row=4, column=1)

# Function to add a number

def button_click(number):
current = enter.get()
enter.delete(0, tkinter.END)
enter.insert(0, str(current) + str(number))
# Function to clear enter box

def button_clear():
enter.delete(0, tkinter.END)

# Function for a decimal button

def button_decimal():
current = enter.get()
if "." not in current:
enter.delete(0, tkinter.END)
enter.insert(0, str(current) + ".")

# Function to add a mathematicaloperation

def button_operation(operation):
global math_operation
math_operation = operation
global first_number
first_number = float(enter.get())
enter.delete(0, tkinter.END)

# Function for equal button

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)

root.mainloop() # Mainloop window

You might also like