0

I know that Python, unlike Java, supports inheritance. But does a user class can inherits from several wxPython class without any problem ? (Does the wxPython design allows this ?)

Thank you in advance

I'm coding under Xubuntu 11.04 with wxPython 2.8 binding

P.S : This is my attempt.

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import wx

class Square(wx.Panel, wx.Control):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY, size=(60,60), pos=(80,50))
        wx.Control.__init__(self, parent)
        self.SetBackgroundColour(wx.Colour(0,0,255))

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Reactive square application",
            size = (300,200))
        panel = wx.Panel(self, wx.ID_ANY)
        square1 = Square(panel)
        square2 = Square(panel)
        square1.Bind(wx.EVT_BUTTON, self.OnSquareClick)

    def OnSquareClick(self, event):
        dialog = wx.MessageDialog(self, "You clicked on square !!!",
            "Hit has been done", wx.OK)
        dialog.Show(True)


if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MainFrame()
    frame.Show(True)
    app.MainLoop()

And this is the stack trace :

swig/python detected a memory leak of type 'wxControl *', no destructor found. Traceback (most recent call last): File "/home/laurent/Documents/Programmation/Projets/Python/SourcesDeTest/ReactiveSquare.py", line 31, in frame = MainFrame() File "/home/laurent/Documents/Programmation/Projets/Python/SourcesDeTest/ReactiveSquare.py", line 19, in init square1 = Square(panel) File "/home/laurent/Documents/Programmation/Projets/Python/SourcesDeTest/ReactiveSquare.py", line 10, in init wx.Control.init(self, parent) File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 11718, in init self._setOORInfo(self) File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 3887, in _setOORInfo args[0].this.own(False) File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 14606, in getattr raise PyDeadObjectError(self.attrStr % self._name) wx._core.PyDeadObjectError: The C++ part of the Square object has been deleted, attribute access no longer allowed. Script terminated.

3 Answers 3

1

You don't really want to do multiple inheritance with wxPython classes UNLESS they're a normal wx class plus a mixin (see g.d.d.c's answer). Or a wxPython class and a user-defined class. Otherwise, you will probably have issues.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, i didn't know about mixin classes. I'm going to search for a good introduction on Google. Because I think that the second solution (wxPython sub + my own class) will also be difficult to make it work
1

Inheritance from multiple parent classes is definitely possible, yes.

http://docs.python.org/tutorial/classes.html#multiple-inheritance

I don't seem to run into any trouble using multiple base classes, wx classes included:

class VirtualList(ListCtrl):
  def __init__(self,
               parent,
               colref = None,
               style = LC_REPORT | LC_VIRTUAL | LC_HRULES | LC_VRULES):

    ListCtrl.__init__(self, parent, style = style)

class TransformList(VirtualList, CheckListCtrlMixin):
  def __init__(self, parent, refid):
    VirtualList.__init__(self, parent, colref = 'transform_columns')

    CheckListCtrlMixin.__init__(self)

    # This facilitates drag / drop re-ordering.
    self.Bind(wx.EVT_LIST_BEGIN_DRAG, self._startDrag)

    dt = ListDrop(self._reorder)

    self.SetDropTarget(dt)

2 Comments

yes, I did know it, as I said in my first post. But it seems that I run into problems trying to applying inheritance with wxPython classes
@LaurentBERNABE - It would allow us to help you more if you demonstrated code of yours that does not seem to work well with multiple inheritance.
1

It is my experience that wxPython does not encourage multiple inheritance of wxPython classes.

Doing something like this will either cause errors or unexpected consequences with your new class:

class MyControl(wxButton, wxComboBox):
    pass

However, you can use multiple inheritence to inherit a wxPython class and your own class in order to extend it in a more OO kind of way.

 class ControlActions(object):
     def MoveHere(self):
          pass

 class MyControl(wxButton, DoActions):
     pass

1 Comment

Hmm, that won't solve my problem. But thank you. In fact, i want to inherit both from wxPanel and wxControl => that way I hope to draw multiple blue squares that reacts to wxEVT_BUTTON event. But I ran into a serious exception. I also tried to use PlateButton, but I did not manage to give it the simple box apperance i wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.