2012-07-31 53 views
0

我有一個wxpython程序,其中我按照教程子類化wx.Dialog。在對話框中,我創建一個面板和一個尺寸。wxPython子類從類中獲取屬性

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屬性從父母傳遞給孩子。

我嘗試了許多變化的面板= StretchDialog.panel和麪板= StretchDialog.InitUI.panel到沒有結束。

是否有可能通過繼承父類來實現wxpython?如果是這樣,當我嘗試訪問面板時如何搞亂命名空間?

回答

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)) 
+0

Wrks奇妙。我將不得不回顧並重新閱讀所有的命名空間文檔,以確保我完全掌握父母和孩子之間的變量傳遞。謝謝! – Jzl5325 2012-07-31 21:56:58