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?
.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 insideget_entry_text(), rather than using empty strings that were retrieved earlier.input()- widgets don't wait for user data but they only inform framework what elements it has to show in window - andmainloop()will create window and show widgets. So.get()used afterEntry()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)