单击按钮后如何将输入值(TKinter)传递给其他类中的函数并使用传递的值运行函数[重复]

Posted

技术标签:

【中文标题】单击按钮后如何将输入值(TKinter)传递给其他类中的函数并使用传递的值运行函数[重复]【英文标题】:How to pass input value(TKinter) to function in other class and run function with passed value after click on button [duplicate] 【发布时间】:2021-02-20 12:01:51 【问题描述】:

单击主窗口中的按钮后,我想在其他类中运行函数。我应该如何将值 [id_entry] 从类 [StartingPage] 中的数据输入传递到第二类 [Graph] 中的函数 [read_data]。单击“分析费用”按钮后,我想传递值并运行功能。有可能吗?

class MainWindow(tk.Tk):

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    tk.Tk.iconbitmap(self)
    tk.Tk.wm_title(self, 'Data analyser')

    window = tk.Frame(self)
    window.pack(side='top', fill='both', expand = True)
    window.grid_rowconfigure(0, weight=1)
    window.grid_columnconfigure(0, weight=1)

    self.frames = 

    for F in (StartingPage, Graph):
        frame = F(window, self)
        self.frames[F] = frame
        frame.grid(row = 0, column = 0, sticky = 'nsew')

    self.show_gui(StartingPage)

def show_gui(self, window):
    frame = self.frames[window]
    frame.tkraise()


class StartingPage(tk.Frame):

def __init__(self, parent, window):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text = 'Upload document')
    label.pack(pady = 10, padx = 10)

    button1 = ttk.Button(self, text = 'Upload XLS file',
                           command=lambda: self.open_file())
    button1.pack()

    button2 = ttk.Button(self, text = 'Analyse expense',
                           command=lambda: window.show_gui(Graph))
    button2.pack()
    id_entry = ttk.Entry(self)
    id_entry.pack()

def get_string(self):
    return self.id_entry.get()

def open_file(self):
    file = askopenfile(mode='r', filetypes=[('excel files', '*.xlsx')])
    export_do_SQL.export_to_sql(file.name)

class Graph(tk.Frame):

def __init__(self, parent, window):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text = 'Expense analyser')
    label.pack(pady = 10, padx = 10)

    button1 = ttk.Button(self, text = 'Return',
                           command=lambda: window.show_gui(StartingPage))
    button1.pack()


    id_element = read_data(DATA FROM INPUT)

【问题讨论】:

【参考方案1】:

您应该在MainWindow 中托管id_entry,因为您已经将它作为parent 传递给其他两个类:

class MainWindow(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self)
        tk.Tk.wm_title(self, 'Data analyser')
    
        window = tk.Frame(self)
        window.pack(side='top', fill='both', expand = True)
        window.grid_rowconfigure(0, weight=1)
        window.grid_columnconfigure(0, weight=1)
    
        self.frames = 
    
        for F in (StartingPage, Graph):
            frame = F(self, window)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = 'nsew')
    
        self.show_gui(StartingPage)
    
    def show_gui(self, window):
        frame = self.frames[window]
        frame.tkraise()


class StartingPage(tk.Frame):

    def __init__(self, parent, window):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = 'Upload document')
        label.pack(pady = 10, padx = 10)
    
        button1 = ttk.Button(self, text = 'Upload XLS file',
                               command=lambda: self.open_file())
        button1.pack()
    
        button2 = ttk.Button(self, text = 'Analyse expense',
                               command=lambda: parent.show_gui(Graph))
        button2.pack()
        self.parent.id_entry = ttk.Entry(self)
        self.parent.id_entry.pack()
    
    def get_string(self):
        return self.parent.id_entry.get()
    
    def open_file(self):
        file = askopenfile(mode='r', filetypes=[('excel files', '*.xlsx')])
        export_do_SQL.export_to_sql(file.name)

class Graph(tk.Frame):

    def __init__(self, parent, window):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = 'Expense analyser')
        label.pack(pady = 10, padx = 10)
    
        button1 = ttk.Button(self, text = 'Return',
                               command=lambda: parent.show_gui(StartingPage))
        button1.pack()
    
    
        id_element = read_data(DATA FROM INPUT)
    def read_data(self):
        # use self.parent.id_entry here

还要注意我是如何交换 parentwindow 的,这样parent 指的是父类,window 指的是在MainWindow 中创建的tk.Frame()

【讨论】:

当我运行它时,有:self.parent.id_entry = ttk.Entry(self) AttributeError: 'StartingPage' object has no attribute 'parent' 好吧,也许你的两个班也需要self.parent = parent

以上是关于单击按钮后如何将输入值(TKinter)传递给其他类中的函数并使用传递的值运行函数[重复]的主要内容,如果未能解决你的问题,请参考以下文章

登录按钮按下后,如何获取Tkinter entry的值并传递给SQL查询?

如何将值从其他活动传递给 viewpager 片段?

如何正确地将单击按钮属性值传递给表单提交事件?

按钮点击后,Python tkinter在框架中显示文本

如何从小部件的函数返回值,并将其传递给 Tkinter,Python 中的另一个小部件的函数

如何将模型动作传递给 Tkinter MVC 中的查看按钮?