Python 父引用问题

Python 父引用问题,python,class,wxpython,Python,Class,Wxpython,我有一个wxPython应用程序,其中包含如下代码。我想设置MyFrame类的属性值,但无法引用它。 我怎样才能使这个代码工作 class MyFrame1(wx.Frame): def __init__(self, *args, **kwds): wx.Frame.__init__(self, *args, **kwds) self.gauge_1 = wx.Gauge(self, -1) self.notebook_1=myNotebo

我有一个wxPython应用程序,其中包含如下代码。我想设置MyFrame类的属性值,但无法引用它。
我怎样才能使这个代码工作

class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.gauge_1 = wx.Gauge(self, -1)
        self.notebook_1=myNotebook(self, -1)

class myNotebook(wx.Notebook):
    def __init__(self, *args, **kwds):
        wx.Notebook.__init__(self, *args, **kwds)
        self.other_class_1=other_class()
        self.other_class_1.do_sth()

class other_class(object):
    def do_sth(self):
        gauge_1.SetValue(value) #doesn't work of course, how do I do this?

试着这样做:

class other_class(object):
     def __init__(self):
        self.g1=MyFrame1()
    def do_sth(self):
        self.g1.gauge_1.SetValue(value)

试着这样做:

class other_class(object):
     def __init__(self):
        self.g1=MyFrame1()
    def do_sth(self):
        self.g1.gauge_1.SetValue(value)

我认为,对于一个子UI元素来说,要了解其父元素的具体情况有点糟糕。这是一个倒退的设计。孩子们通常应该有一些方式来发出信号或提出一个事件,让适当的听者做出反应。但是,如果这真的是您想要做的,那么您可能希望获得父项并直接对其执行操作

注意:不要使用这种方法。我正在说明为什么设计有问题

首先,你甚至不能用代码的结构来做,因为
other_class
没有对父类的引用。这是一个通用实例。所以你必须做一些像

class other_class(object):

    def __init__(self, parent):
        self.parent = parent
在你的笔记本课上

class myNotebook(wx.Notebook):
    def __init__(self, *args, **kwds):
        wx.Notebook.__init__(self, *args, **kwds)
        # note here we pass a reference to the myNotebook instance
        self.other_class_1 = other_class(self)
        self.other_class_1.do_sth()
一旦你的另一个类知道了它的父类,你必须让父类的父类拥有MyFrame1实例

class other_class(object):

def __init__(self, parent):
    self.parent = parent

def do_sth(self, value):
    self.parent.GetParent().gauge_1.SetValue(value) 
你现在明白为什么这是个糟糕的设计了吗?多个级别的对象必须假定了解父结构

我的wxPython还没有准备好,所以我不能给你具体的信息,但这里有一些可能的通用方法可以考虑:

  • 确定
    other_class
    的角色到底是什么。如果它真的要在MyFrame1的子级上操作,那么该功能属于MyFrame1,因此它可以了解这些成员
  • 如果
    other_class
    是wx对象,则调用
    do_sth()
    方法时,它可能会发出wx.Event。您可以在MyFrame1或Notebook级别绑定该事件,并在处理程序中执行所需的任何工作

  • 我认为,对于一个子UI元素来说,要了解其父元素的具体情况有点糟糕。这是一个倒退的设计。孩子们通常应该有一些方式来发出信号或提出一个事件,让适当的听者做出反应。但是,如果这真的是您想要做的,那么您可能希望获得父项并直接对其执行操作

    注意:不要使用这种方法。我正在说明为什么设计有问题

    首先,你甚至不能用代码的结构来做,因为
    other_class
    没有对父类的引用。这是一个通用实例。所以你必须做一些像

    class other_class(object):
    
        def __init__(self, parent):
            self.parent = parent
    
    在你的笔记本课上

    class myNotebook(wx.Notebook):
        def __init__(self, *args, **kwds):
            wx.Notebook.__init__(self, *args, **kwds)
            # note here we pass a reference to the myNotebook instance
            self.other_class_1 = other_class(self)
            self.other_class_1.do_sth()
    
    一旦你的另一个类知道了它的父类,你必须让父类的父类拥有MyFrame1实例

    class other_class(object):
    
    def __init__(self, parent):
        self.parent = parent
    
    def do_sth(self, value):
        self.parent.GetParent().gauge_1.SetValue(value) 
    
    你现在明白为什么这是个糟糕的设计了吗?多个级别的对象必须假定了解父结构

    我的wxPython还没有准备好,所以我不能给你具体的信息,但这里有一些可能的通用方法可以考虑:

  • 确定
    other_class
    的角色到底是什么。如果它真的要在MyFrame1的子级上操作,那么该功能属于MyFrame1,因此它可以了解这些成员
  • 如果
    other_class
    是wx对象,则调用
    do_sth()
    方法时,它可能会发出wx.Event。您可以在MyFrame1或Notebook级别绑定该事件,并在处理程序中执行所需的任何工作

  • 我觉得如果不先解释一下
    other_class
    的角色,你就无法得到一个合适的答案?它真的应该是一个泛型类,保存对MyFrame实例的引用吗?这里的用法是什么?据我们所知,MyFrame1可以有一个全局实例,可以由
    其他类
    实例直接访问。我在仔细观察之后才意识到
    其他类
    是什么。奇怪的是,我觉得如果不先解释一下
    other_class
    的角色,你就无法得到一个合适的答案?它真的应该是一个泛型类,保存对MyFrame实例的引用吗?这里的用法是什么?据我们所知,MyFrame1可以有一个全局实例,可以由
    其他类
    实例直接访问。我在仔细观察之后才意识到
    其他类
    是什么。谢谢你清晰而广泛的回答。我知道我不应该这样做,但最终我使用了
    self.parent.GetParent()
    方式。我已经有太多的代码来完全重新设计我的程序。不过,在编写未来的程序时,我会记住您的提示,希望以后的程序设计会更好。谢谢您明确而广泛的回答。我知道我不应该这样做,但最终我使用了
    self.parent.GetParent()
    方式。我已经有太多的代码来完全重新设计我的程序。不过,在编写未来的程序时,我会记住您的提示,希望以后的程序会有更好的设计。