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?
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?
Share Improve this question asked Mar 8 at 1:36 Ahadul IslamAhadul Islam 1 2 |1 Answer
Reset to default 2You 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')
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744905555a4600232.html
.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. – jasonharper Commented Mar 8 at 2:50input()
- 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) – furas Commented Mar 8 at 9:51