Accessing Entry widget information: tkinter python
I am writing a Tkinter program and have a root where a labels are displayed next to entry widgets.
EX: Name -> SearchTerm ENTRY BOX
I am trying to create a list of values from entry wi开发者_开发百科gets (EX: ['', 'user input', 'user input'])
I have created the entry widgets in the root and then have a function that gets the values:
def get_user_entries(user_entries):
new_search_terms = []
for entry in user_entries:
new_search_terms.append(entry.get())
return new_search_terms
I want to be able to access the list called new_search_terms in order to run another python program on it.
I have tried to use the function as the command for a button. I have tried binding the button in different places. I have tried invoking the button in different places. I also tried creating a class where I new_search_terms was a class variable, but since I can't access the returned information from the function above the updated class variable did not hold. After exhausting the internet and several books I have yet to find an example where information is returned out of a Button function rather than printed. I need to be able to access this information in another place.
Is there a way to access the information I need or is there another approach to this in Tkinter I haven't thought of?
The code where I create the button:
done = Button(root, text='Done', command= lambda: get_user_entries(user_entries))
done.pack()
Here is the code that created the entries and labels:
def uc_1(original_names, final_names, root):
user_entries = []
for index in range(len(original_names)):
row = Frame(root)
info = Label(row, text = original_names[index]+'->'+final_names[index])
user_entry = Entry(row)
row.pack(side=TOP, fill=X)
user_entry.pack(side=RIGHT, expand=YES, fill=X)
user_entries.append(user_entry)
info.pack(side=LEFT)
return user_entries
Thanks!
Button commands don't "return". They do, but they return to the event loop so there's nothing there to process whatever it is that is returned.
That being said, it's hard to know what you are asking. You say "I can't access the returned information from the function" but I see no reason why not. If you call it, does it not return what the return statement returns? That seems quite impossible.
If you configure a button to run 'self.OnButton', and define 'self.OnButton' to call your function 'get_user_entries', what happens? Does it not retun anything?
Look at it this way: your button function shouldn't return something, it should do something. It can do that by calling you function, get what it returns, tnen call whatever else you want it to call.
精彩评论