0

I am trying to get the input text from an entry and based upon the length of the text, perform some other things. My code looks like this:

entry = tk.Entry(root)
entry.grid(row=8, column=7, rowspan=2)
entry_text = entry.get().upper()
print(x)
button = tk.Button(root, text="Enter the Word", command= lambda: get_entry_text(entry_text, i_label_list))
button.grid(row=8, column=8, rowspan=2)

My lambda function looks like this:

def get_entry_text(entry_value, i_label_list):
    global no_of_attempts
    print(len(entry_value))
    if len(entry_value) == 6:
        no_of_attempts += 1
        for i in range(6):
            i_label_list[no_of_attempts][i].config(text=entry_value[i])
    else:
        print('Invalid Length')

What I have discovered is that the lambda function is not getting/passing the actual text in the entry. Rather, it is getting/passing empty text to the get_entry_text function, which results in 'invalid length' no matter which text I pass.

I have tried partial from functools to pass the values, but to no avail.

What am I doing wrong?

2
  • 2
    Calling .get() on an Entry immediately after creating it is going to produce an empty string, of course - where could any other value possibly come from? And that empty string isn't going to magically change into something else just because the user typed into the Entry later. You need to do all the .get() calls inside get_entry_text(), rather than using empty strings that were retrieved earlier. Commented Mar 8 at 2:50
  • GUIs (like Tkinter, PyQt, wxPython and other also in other languages) don't work like standard input() - widgets don't wait for user data but they only inform framework what elements it has to show in window - and mainloop() will create window and show widgets. So .get() used after Entry() is useless because it is executed before it even create window (and before user can see it and put data). If you need to get or set some values then you have to do inside function assigned to button's click (or to other events) Commented Mar 8 at 9:51

1 Answer 1

1

You should try entering the text/value inside the function like below (suggested by others as well).
"Invalid Length" will appear always if the entered text is not of length 6 everytime as per the code.

def get_entry_text(entry_value, i_label_list):
    global no_of_attempts
    entry_text = entry_value.get().upper()
    print(len(entry_text))
    if len(entry_text) == 6:
        no_of_attempts += 1
        for i in range(6):
            i_label_list[no_of_attempts][i].config(text=entry_text[i])
    else:
        print('Invalid Length')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.