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()
CodePudding user response:
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()