0

我有一个 wxpython 程序,我按照教程将 wx.Dialog 子类化。在对话框中,我创建了一个面板和一个 sizer。

class StretchDialog(wx.Dialog):

'''A generic image processing dialogue which handles data IO and user interface.
This is extended for individual stretches to allow for the necessary parameters
to be defined.'''

def __init__(self, *args, **kwargs):
    super(StretchDialog, self).__init__(*args, **kwargs)

    self.InitUI()
    self.SetSize((600,700))

def InitUI(self):
    panel =  wx.Panel(self)
    sizer = wx.GridBagSizer(10,5)

块注释描述了我要实现的功能,本质上是使用它作为基础动态生成更复杂的对话框。为此,我尝试过:

class LinearStretchSubClass(StretchDialog):
'''This class subclasses Stretch Dialog and extends it by adding 
    the necessary UI elements for a linear stretch'''

def InitUI(self):
    '''Inherits all of the UI items from StretchDialog.InitUI if called as a method'''
    testtext = wx.StaticText(panel, label="This is a test")
    sizer.Add(testtext, pos=(10,3))

我通过 InitUI 方法调用子类以便能够扩展,但不会覆盖父类的 InitUI 中的 UI 生成。我无法做的是将面板和可能的 sizer 属性从父级传递给子级。

我尝试了 panel = StretchDialog.panel 和 panel = StretchDialog.InitUI.panel 的许多变体,没有尽头。

是否可以通过子类化父级在 wxpython 中实现这一点?如果是这样,我在尝试访问面板时如何弄乱命名空间?

4

1 回答 1

1

您在子类中的 InitUI 导致 InitUI 不在 StretchDialog 中调用

你可以这样做

class StretchDialog(wx.Dialog):

    '''A generic image processing dialogue which handles data IO and user interface.
      This is extended for individual stretches to allow for the necessary parameters
      to be defined.'''

    def __init__(self, *args, **kwargs):
        super(StretchDialog, self).__init__(*args, **kwargs)

        self.InitUI()
        self.SetSize((600,700))

   def InitUI(self):
       #save references for later access
       self.panel =  wx.Panel(self)
       self.sizer = wx.GridBagSizer(10,5)

然后在你的孩子班

class LinearStretchSubClass(StretchDialog):
'''This class subclasses Stretch Dialog and extends it by adding 
the necessary UI elements for a linear stretch'''

    def InitUI(self):
    '''Inherits all of the UI items from StretchDialog.InitUI if called as a method'''
         StretchDialog.InitUI(self) #call parent function
         testtext = wx.StaticText(self.panel, label="This is a test")
         self.sizer.Add(testtext, pos=(10,3))
于 2012-07-31T21:46:30.910 回答