2011-10-06 67 views
0

我知道Python和Java不同,它支持继承。但是用户类可以从几个wxPython类继承而没有任何问题? (是否wxPython的设计让这个?)wxPython和多重继承

预先感谢您

我是Xubuntu 11.04之下编码与wxPython的2.8结合

P.S:这是我的尝试。

#!/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() 

这是堆栈跟踪:从多个父类

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.

回答

1

你不是真的想这样做多重继承与wxPython的类,除非他们正常的WX类加一个混合(见g.d.d.c的答案)。或者一个wxPython类和一个用户定义的类。否则,你可能会遇到问题。

+0

好吧,我不知道mixin类。我要在Google上搜索一个很好的介绍。因为我认为第二个解决方案(wxPython sub +我自己的类)也很难使其工作 – loloof64

1

继承是绝对有可能的,是的。

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

我似乎没有碰到使用多个基类的任何麻烦,WX类包括:

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

是的,我确实知道,就像我在第一篇文章中所说的那样。但似乎遇到了问题,试图通过wxPython类来应用继承 – loloof64

+0

@LaurentBERNABE - 如果你演示了你的代码似乎不适合多重继承,它将允许我们更多地帮助你。 –

+0

好吧,我在我的第一个代码中加入了我的代码 – loloof64

1

这是我的经验,wxPython的不鼓励的wxPython类的多重继承。

做这样的事情要么导致错误或意外的结果与新类:

class MyControl(wxButton, wxComboBox): 
    pass 

但是,您可以使用多个的传承,以继承的wxPython类和你自己的类,以它的扩展更多的OO种方式。

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

class MyControl(wxButton, DoActions): 
    pass 
+0

嗯,那不会解决我的问题。但是,谢谢。实际上,我想从wxPanel和wxControl =>继承这两种方式,我希望绘制多个对wxEVT_BUTTON事件有反应的蓝色方块。但我遇到了一个严重的例外。我也尝试使用PlateButton,但我没有设法给它简单的盒子外观我想要的。 – loloof64