How to retrieve user input from Entry using Button Tkinter?

Question:

I’m trying to build this small window using Tkinter where people upload a link and it just returns it (the project is more complex but let’s simplify).

This is the function that just prints the link string:

tag_list = []

def add_product_link(link):
    global tag_list
        
    product_tag = link[32:]
    tag_list.append(product_tag)
    
    print(tag_list)

Basically people put links in the text box and each time they press the "Add Link" button it adds a bit of the url in the tag_list list.

Here is my Tkinter code:

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        
        label = tk.Label(self, text=('Enter the product links below !'), font=MEDIUM_FONT)
        label.grid(row=0, column=0, columnspan=2, padx=10, pady=10)
          
        user_entry_text = tk.Label(self, text=('Link: '), font=MEDIUM_FONT)
        user_entry_text.grid(row=1, column=0, sticky=tk.W, padx=10, pady=10)
        
        user_entry = tk.Text(self, width=30, height=2)
        user_entry.grid(row=1, column=1, sticky=tk.E, padx=10, pady=10)        
        
        #this button is problematic
        quit_button = tk.Button(self, text=('Quit'), command=self.destroy)
        quit_button.grid(row=2, column=1, sticky=tk.E, padx=10, pady=10)
        
        url = user_entry.get()
        add_button = tk.Button(self, text=('Add Item'), command=lambda: add_product_link(url))
        add_button.grid(row=2, column=0, sticky=tk.W, padx=10, pady=10)

For some reason the user_entry.get() does not retrieve what I put in entry.

Thank you !

Asked By: bsaoptima

||

Answers:

If you have no other classes that need access to the add_product_link function, you may have an easier time moving it inside of the StartPage class like so

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        
        self.label = tk.Label(self, text=('Enter the product links below!'), font=MEDIUM_FONT)
        self.label.grid(row=0, column=0, columnspan=2, padx=10, pady=10)
          
        self.user_entry_text = tk.Label(self, text=('Link: '), font=MEDIUM_FONT)
        self.user_entry_text.grid(row=1, column=0, sticky=tk.W, padx=10, pady=10)
        
        self.user_entry = tk.Text(self, width=30, height=2)
        self.user_entry.grid(row=1, column=1, sticky=tk.E, padx=10, pady=10)        
        
        #this button is problematic
        self.quit_button = tk.Button(self, text=('Quit'), command=self.destroy)
        self.quit_button.grid(row=2, column=1, sticky=tk.E, padx=10, pady=10)
        
        # url = user_entry.get()
        # modify the button's command here
        self.add_button = tk.Button(self, text=('Add Item'), command=self.add_product_link)
        self.add_button.grid(row=2, column=0, sticky=tk.W, padx=10, pady=10)

        self.tag_list = []  # make tag list available to this class

    def add_product_link(self):
        link = self.user_entry.get()  # get the entry contents
        product_tag = link[32:]
        self.tag_list.append(product_tag)
        print(self.tag_list)

If you do need access to add_product_link, you could define it as a method inside your application’s root class (which I assume is the controller for your StartPage class). You’ll want to move self.tag_list into your root class. You can then call the method from the button in StartPage via command=controller.add_product_link (instead of self.add_product_link)

You can also access the tag_list from other classes that you pass controller to, via controller.tag_list

Answered By: JRiggles
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.