将变量从 tkinter GUI 返回到函数

Returning a variable from a tkinter GUI to a function

我正在尝试从 tkinter GUI 输入字段中获取一些条目,然后用于我脚本中的其他功能,但我似乎不知道如何获取这些值。我想获取在条目字段 entry1 和 entry2 中键入的字符串,然后使用 entry2 执行位于我的界面 class 之外的抓取功能。 但是我似乎无法访问我的函数的 entry2 值。

到目前为止,这是我的代码:

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()

您可以将 entry2 值作为参数传递给函数,如代码所示:

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()