Python 3 个不同的 tkinter 按钮命令里面 class

Python 3 tkinter button command inside different class

我正在创建一个 Python (3.4.3) - tkinter 程序,我想知道是否可以从另一个 [=] 中引用一个 def (self.get_details) 16=] 用于按钮的命令。我在任何地方都找不到这个问题的答案,所以我想我只是问问。

示例:

import tkinter as tk
...

class Widgets(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init___(self, parent)
        self.parent = parent

        self.initUI()

    def initUI():

        # Lots of other different tkinter widgets go here

        self.button = tk.Button(command=App(get_details))
        self.button.pack()


class PopUp(tk.TopLevel): ....

class App(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.initUI()

    def get_details(self):

        # Complete a function

    def initUI(self):

        self.parent.title("My Application")
        self.style = Style()
        self.style.theme_use("default")

        self.pack()

        self.widgets = Widgets(self)
        self.widgets.pack(side="top", anchor="center", fill="both", expand=True)

if __name__ == "__main__":

    root = tk.Tk()
    App(root).pack(side="top", fill="both", expand=True)
    root.resizable(0,0)
    root.mainloop()

所以我想要一个属于 class Widgets() 的按钮来调用属于 class App() 的命令 def get_details(self) ,其中包含 Widgets() class。

我希望我描述的足够多,这个问题很难描述。总的来说,我对 Python 还是有点陌生​​。谢谢!

编辑:

按照建议,我将其更改为 self.parent.get_details(),效果很好!但是,当我从 def get_details() 中的 Widgets() class 引用一个 tkinter 小部件时,例如:self.button,我得到:

AttributeError: 'App' object has no attribute 'button'

所以我尝试将按钮引用为:self.parent.button,我收到了:

AttributeError: 'tkapp' object has no attribute 'button'

我应该如何成为 calling/referencing 按钮?谢谢!

App 中,您创建了一个 Widgets 对象并保存了一个名为 self.widgets 的引用。当您这样做时,您将对该 App 对象的引用传递给 Widgets 对象。 App 实例引用在 Widgets 对象中保存为 self.parent。请记住,self 的这两种用法指的是不同的对象:而在 App 中,self 指的是 App 对象的当前实例。而在 Widgets 中,self 指的是 Widgets 对象的当前实例。要引用另一个 class 中的内容,您必须使用正确的引用并遵循路径。

在一个 App 实例中:

self             this App instance
.widgets         the Widgets instance it contains
.button          the button in that Widget instance

Widgets 实例中:

self             this Widgets instance
.parent          the App object that created this Widgets object
.get_details     the get_details function in the parent App object

在下文中,我修复了您的代码的剩余位(... 导致语法错误,Style 未定义等)并提供了一个小示例来说明如何从中引用每个对象另一个。该按钮最初显示 "hello",您可以通过单击将其更改为 "goodbye"。

import tkinter as tk

class Widgets(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.initUI()

    def initUI(self):

        # Lots of other different tkinter widgets go here

        self.button = tk.Button(text='hello', command=self.parent.get_details)
        self.button.pack()

class App(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.initUI()

    def get_details(self):

        self.widgets.button.config(text='goodbye')

    def initUI(self):

        self.parent.title("My Application")

        self.pack()

        self.widgets = Widgets(self)
        self.widgets.pack(side="top", anchor="center", fill="both", expand=True)

if __name__ == "__main__":

    root = tk.Tk()
    App(root).pack(side="top", fill="both", expand=True)
    root.resizable(0,0)
    root.mainloop()