I would like to get the "Ok" print of a number (number = 2 2) if I compare this number with a number that I insert in a textbox.
The first number is
number1 = 2 2
The second number is saved in a database and is displayed in a textobox
number2 = Entry (root, width = 4, highlightthickness = 0, bg = "# ffffc0")
number2.place (x = 1, y = 1)
Comparison in this way:
if number1 > number2:
print ("Ok")
I get TypeError: '>' not supported between instances of 'float'
How can I solve? Thank you
CodePudding user response:
The call to Entry()
returns an object which is a text widget and is stored in number2
. To get its contents, you need to call its get()
method. The result will be of type str
.
You will need to call int(number2.get())
or float(number2.get())
to get a variable whose type is compatible with that of the variable number1
shown in your question.
CodePudding user response:
Store the data (textvariable
) of Entry widget in a StringVar
variable, then while comparing typecast it to float
, int
or double
. (For double
values, you can use DoubleVar
also.)
Refer to the documentation, to know more.
Like this:
number2=StringVar()
number2EntryBox = Entry (root,textvariable=number2, width = 4, highlightthickness = 0, bg = "# ffffc0")
number2EntryBox.place (x = 1, y = 1)
Now compare like this:
if number1 > float(number2.get()):
print ("Ok")