Help needed with Text box UI

Let’s take this program for example

User_Input = int(input("Enter a number: "))

if User_Input == "2":

Now this program checks if the input entered by the user is equal to 2

My question is I tried to do the same thing with text boxes by doing this:

TextEntry1 = Entry(Main_Area, width=50, borderwidth=5).grid(row=3, column=2)

if TextEntry1 == "2":

But this program would give a syntax error

Anyway of checking the input done by the user using text boxes?

Actually, no, it doesn’t—but its actually a great analogy for what’s happening below. The user’s input is treated as a string, e.g. "2", and you then convert it to an integer number, e.g. 2, with int(). However, in your if block, you are checking if it is the string "2" rather than the integer 2, which are not the same thing, just like writing the character "2" on a piece of paper is not the same as having two apples (or two of anything). You should either check if User_Input is the integer 2 in your if block, or don’t convert it to int() when you read it in, depending on your requirements.

It would be helpful if you provided a more complete example, and in particular provided the actual full error message and traceback you are getting, as otherwise we are left to merely guess at what is going on. Entry() could be anything, but I’m guessing you’re talking about a tkinter.ttk.Entry.

While there could (and likely are) other issues here, the most relevant to your question follows directly from the above. As before, TextEntry is not a string, like the "2" that it is being compared to, but rather a tkinter.ttk.Entry object. In order to get the result as a string, you need to call the .get() method on the object once the user has responded to it, plus some manipulation. A quick Google revealed a potentially useful Stack Overflow answer discussing more, and you might also want to read the documentation.

(As a side note, searching Google for your problem is often a very useful tool, so long as you try to learn from what you find rather than simply copying and pasting.)

Best of luck!

1 Like

Ohh ok so it is not as easy as I thought. Anyways thanks for the help. The stackoverflow answer did help me in understanding what you were trying to say. While writing this post I made a error of making the ‘2’ a string actually it should be a integer. Anyways thanks for this help, this indeed did help me. Thanks for wishing me good luck. Have a nice day/night :grinning:

1 Like