2011-10-06 66 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