Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning a variable from a tkinter GUI to a function

I am trying to get some entries from tkinter GUI entry fields to then use for other functions in my script, but I can’t seem to know how to grab those values. I want to get the strings that are typed in the entry fields entry1 and entry2 and then use the entry2 to perform a scraping function that lives outside my Interface class.
But I can’ seem to access the entry2 value for my function.

Here is my code so far:

from tkinter import *
from tkinter import ttk
from requests_html import HTMLSession


s = HTMLSession()

def thomann_scrape(thomannURL):
    r = s.get(URL)
    print(r)
    return r

class Interface:

    def on_button(self):
        self.productID = self.entry1.get()
        self.thomannURL = self.entry2.get()

    def __init__(self, master):
        self.master = master
        self.productID = None
        self.thomannURL = None

        master.title("New Product from Thomann - Audio Pro CMS")

        self.mainframe = ttk.Frame(master, padding="3 3 12 12")
        self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        master.columnconfigure(0, weight=1)
        master.rowconfigure(0, weight=1)
        
        
        self.entry1 = StringVar()
        self.entry1_entry = ttk.Entry(self.mainframe, textvariable=self.entry1, width=20)
        self.entry1_entry.grid(column=2, row=1, sticky=(W, E))
        
        self.entry2 = StringVar()
        self.entry2_entry = ttk.Entry(self.mainframe, textvariable=self.entry2, width=20)
        self.entry2_entry.grid(column=2, row=2, sticky=(W, E))
        
        
        ttk.Label(self.mainframe, text="Enter Product ID:").grid(column=1, row=1, sticky=E)
        ttk.Label(self.mainframe, text="Enter Thomann product URL:").grid(column=1, row=2, sticky=E)
        ttk.Button(self.mainframe, text="Run", command=self.on_button).grid(column=3, row=3, sticky=W)


        for child in self.mainframe.winfo_children(): 
            child.grid_configure(padx=5, pady=10)

        self.entry1_entry.focus()
        
        master.bind("<Return>", self.on_button)
    
    
root = Tk()
i = Interface(root)
thomann_scrape()
root.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can pass entry2 value as parameter to function as shown in code:

from tkinter import *
from tkinter import ttk
from requests_html import HTMLSession


s = HTMLSession()


def thomann_scrape(thomannURL):
    r = s.get(thomannURL)
    print(r)
    return r


class Interface:

    def on_button(self):
        self.productID = self.entry1.get()
        self.thomannURL = self.entry2.get()
        # Pass parameter(self.thomannURL) to function (thomann_scrape)
        thomann_scrape(self.thomannURL)

    def __init__(self, master):
        self.master = master
        self.ident = None
        self.url = None

        master.title("New Product from Thomann - Audio Pro CMS")

        self.mainframe = ttk.Frame(master, padding="3 3 12 12")
        self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        master.columnconfigure(0, weight=1)
        master.rowconfigure(0, weight=1)

        self.entry1 = StringVar()
        self.entry1_entry = ttk.Entry(
            self.mainframe, textvariable=self.entry1, width=20)
        self.entry1_entry.grid(column=2, row=1, sticky=(W, E))

        self.entry2 = StringVar()
        self.entry2_entry = ttk.Entry(
            self.mainframe, textvariable=self.entry2, width=20)
        self.entry2_entry.grid(column=2, row=2, sticky=(W, E))

        ttk.Label(self.mainframe, text="Enter Product ID:").grid(
            column=1, row=1, sticky=E)
        ttk.Label(self.mainframe, text="Enter Thomann product URL:").grid(
            column=1, row=2, sticky=E)
        ttk.Button(self.mainframe, text="Run", command=self.on_button).grid(
            column=3, row=3, sticky=W)

        for child in self.mainframe.winfo_children():
            child.grid_configure(padx=5, pady=10)

        self.entry1_entry.focus()

        master.bind("<Return>", self.on_button)


root = Tk()
i = Interface(root)

root.mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading