I was trying to create a code to create a GUI Calculator with buttons and I tried looking up Tkinter help, but I was not able to get help. How do I incorporate the commented out section at the bottom to display the value of the numbers shown? Please feel free to add suggestions to my code.
EDIT: I fixed the code as @abarnert suggested but when I run the code, it always writes Invalid Operation. I do not know how to fix this. Does anyone have a better idea to fix my code? I just need to get the operation to work. My edited code is below.
from math import sqrt
from tkinter import *
window = Tk()
window.title("Welcome to Calculator ")
window.configure(background = "white")
Label (window, text = "Calculator", bg = "white") .grid(row = 0, column =
0, sticky = N)
#click function
def click():
n = n_textentry.get()
m = m_textentry.get()
operation = operation_textentry.get()
if operation == 1:
print(n + m)
elif operation == 2:
print(n - m)
elif operation == 3:
print(n * m)
elif operation == 4:
print(n / m)
elif operation == 5:
print(n ** m)
elif operation == 6:
print(sqrt(n))
else:
print("Invalid Operation ")
#to create the box for the first number and store it
Label (window, text = "Enter the first number", bg = "white") .grid(row =
1, column = 0, sticky = N)
n_textentry = Entry(window, width = 10, bg = "white")
n_textentry.grid(row = 2, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 3,
column = 0, sticky = N)
#to create the box for the second number
Label (window, text = "Enter the second number", bg = "white") .grid(row =
5, column = 0, sticky = N)
m_textentry = Entry(window, width = 10, bg = "white")
m_textentry.grid(row = 6, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 7,
column = 0, sticky = N)
#to show list of options
Label (window, text = ''' Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for division
Enter 5 for exponentiation
Enter 6 for square root *This will only work for 1st choice*''', bg =
"white") .grid(row = 9, column = 0, sticky = W)
operation_textentry = Entry(window, width = 10, bg = "white")
operation_textentry.grid(row = 10, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 11,
column = 0, sticky = N)