Reputation: 13
I am creating a code to create a calculator but I keep on getting this error:
Traceback (most recent call last):
File "C:\Users\Monish Shah\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\Monish Shah\AppData\Local\Programs\Python\Python36-
32\monish-play\calc-completed-copy-for-editing-copy2.py", line 40, in click
Label (window, text = str(sqrt(n_textentry)), bg = "white") .grid(row = 13,
column = 0, sticky = N)
TypeError: must be real number, not Entry
Does anyone know why my code does not work? I don't really understand why it cannot be and entry since I am collecting the user's input? I was researching but I could not figure out how to correctly incorporate the user's input into the code.
Here is my code that I used:
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)
#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)
#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)
#click function
def click():
n_textentry.get()
m_textentry.get()
operation_textentry.get()
if operation_textentry == 1:
result1 = Label (window, text = str(n_textentry + m_textentry), bg =
"white") .grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 2:
Label (window, text = str(n_textentry - m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 3:
Label (window, text = str(n_textentry * m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 4:
Label (window, text = str(n_textentry / m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
elif operation_textentry == 5:
Label (window, text = str(n_textentry ** m_textentry), bg = "white")
.grid(row = 13, column = 0, sticky = N)
else:
Label (window, text = str(sqrt(n_textentry)), bg = "white")
.grid(row = 13, column = 0, sticky = N)
# operation_textentry == 6:
# Label (window, text = str(sqrt(n_textentry)), bg = "white")
.grid(row = 13, column = 0, sticky = N)
#else:
# print("Invalid Operation ")
#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)
Upvotes: 0
Views: 7988
Reputation: 366083
There are a number of problems with this code:
get
calls.float
or int
.Label
once at startup, and config
the text in this callback, instead of creating a new Label
every time the user hits Submit
.result
in the elif
chain, and then use it at the end.I already explained most of this in my answer to your previous question.
The result should look something like this:
result_label = Label(window, text = str(n_textentry ** m_textentry), bg = "white") result_label.grid(row = 13, column = 0, sticky = N)
def click():
n = int(n_textentry.get())
m = int(m_textentry.get())
operation = int(operation_textentry.get())
if operation == 1:
result = n+m
elif operation == 2:
result = n-m
elif operation == 3:
result = n*m
elif operation == 4:
result = n/m
elif operation == 5:
result = n**m
else:
result = "Invalid Operation"
result_label.config(text=str(result))
As I mentioned before, you probably want some error handling for the case where the user leaves one of the entries blank, or inputs text instead of a number, or divides by zero, etc. The simplest way to do this with a try:
around the whole click
function:
def click():
try:
n = int(n_textentry.get())
# etc.
except Exception as e:
result_label.config(text=repr(e))
Upvotes: 1
Reputation: 1574
Try converting user input to a float
or int
. It looks like you're trying to apply a mathematical operation on user input (the Entry
object), but mathematical operations are not supported for Entry
objects.
Upvotes: 1