Calling Class' Child Methods

Andrea Gavana andrea_gavana at tin.it
Sun Nov 6 01:05:09 CET 2005


Hello NG,

    this may seem a stupid (or even impossible) question, but my knowlegde
of Python is quite limited. I have basically a simple graphical user
interface that contains a Panel, another panel (child of the main panel) and
a custom widget (child of the main panel). Basically is something like (only
some small code, is in wxPython but the question is more Python-related):

class MainClass(wx.Panel):

    def __init__(self, *args, **kwds):

        wx.Panel.__init__(self, *args, **kwds)

        self.childpanel = wx.Panel(self, -1)
        self.customwidget = Custom(self, -1)

        layoutsizer = wx.BoxSizer(wx.VERTICAL)
        layoutsizer.Add(self.childpanel, 1)
        layoutsizer.Add(self.customwidget)
        layoutsizer.Layout()


The class "Custom" has a lot of methods (functions), but the user won't call
directly this class, he/she will call the MainClass class to construct the
GUI app. However, all the methods that the user can call refer to the
"Custom" class, not the MainClass class. That is, the methods that the user
call should propagate to the "Custom" class. However, I know I can do:

# Inside MainClass
    def SomeMethod(self, param):
        self.customwidget.SomeMethod(param)

But the "Custom" class has *a lot* of methods, so I will end up in rewriting
all the "SomeMethods" in the MainClass just to pass the parameters/settings
to self.customwidget. Moreover, I know I can do (in the __init__ method of
MainClass):

    def __init__(self, *args, **kwds):

        wx.Panel.__init__(self, *args, **kwds)
        Custom.__init__(self, parent, -1)

In order to make MainClass knowing about the Custom methods. But the I will
not be able (I suppose) to add self.customwidget to a layoutsizer. How can I
write:

layoutsizer = wx.BoxSizer(wx.VERTICAL)
layoutsizer.Add(self.childpanel, 1)
layoutsizer.Add(self)   # <=== That's impossible
layoutsizer.Layout()

?

So (and I am very sorry for the long and maybe complex to understand post,
english is not my mother tongue and I am still trying to figure out how to
solve this problem), how can I let MainClass knowing about the Custom
methods without rewriting all the Custom functions inside MainClass and then
pass the parameters to Custom? Is there a way to "propagate" the methods to
the child class (Custom)?

Thanks for every suggestion, and sorry for the long post.

Andrea.
-- 
"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77





More information about the Python-list mailing list