我有一个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中实现这一点?如果是这样,在尝试访问面板时如何弄乱命名空间?
答案 0 :(得分:1)
子类中的InitUI导致不在StretchDialog中调用InitUI
你可以这样做
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))