简体   繁体   English

wxPython子类从类获取属性

[英]wxPython subclass getting attributes from class

I have a wxpython program where I subclass wx.Dialog as per a tutorial. 我有一个wxpython程序,其中按照教程将wx.Dialog子类化。 Within the dialog I create a panel and a 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)

The block comment describes what functionality I am trying to achieve, essentially dynamically generating more complex dialogs using this as the base. 块注释描述了我要实现的功能,本质上是以此为基础动态生成更复杂的对话框。 To do that I have tried: 为此,我尝试过:

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

I call the subclass via the InitUI method to be able to extend, but not overwrite the UI generation in the parent class' InitUI. 我通过InitUI方法调用子类,以便能够扩展,但不覆盖父类的InitUI中的UI生成。 What I am not able to do is pass the panel and presumably sizer attributes from the parent to the child. 我不能做的是将面板和大小调整器属性从父级传递给子级。

I tried many variations of panel = StretchDialog.panel and panel = StretchDialog.InitUI.panel to no end. 我尝试了panel = StretchDialog.panel和panel = StretchDialog.InitUI.panel的许多变化,没有尽头。

Is it possible to achieve this in wxpython by subclassing a parent? 通过子类的父类可以在wxpython中实现吗? If so, how am I messing up the namespace when trying to access panel? 如果是这样,在尝试访问面板时如何弄乱名称空间?

your InitUI in the child class causes InitUI not to be called in StretchDialog 您在子类中的InitUI导致不会在StretchDialog中调用InitUI

you can do it like this 你可以这样

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)

Then in your child class 然后在你的孩子课上

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM