[英]Redefine an existing function
我像這樣定義PyControl
的子類:
class MyBitmapButton(wx.PyControl):
def __init__(self, parent, id=-1, bmp=None, label='blah', pos = wx.DefaultPosition, size=(166,220), style = 0, validator = wx.DefaultValidator,
name = "mybitmapbutton"):
style |= wx.BORDER_NONE
wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
self.myimg = wx.StaticBitmap(self, -1, bmp, pos=(8,8), size=(150,150))
self.mytxt = wx.StaticText(self, -1, label, (6,165))
def Bind(self, *args, **kwargs):
self.Bind(*args, **kwargs) # infinite recursion problem !
self.myimg.Bind(*args, **kwargs)
self.mytxt.Bind(*args, **kwargs)
我想覆蓋標准的Bind
,但是在此定義中,我需要使用舊的 Bind(由wx.PyControl
提供)。
使用當前代碼,我遇到了infinite recusion loop
問題:
如何重用舊 Bind
在新的定義Bind
?
您需要在此處使用super
,以訪問父類的Bind
版本:
super(MyBitmapButton, self).Bind(*args, **kwargs)
或者,在Python 3中,
super().Bind(*args, **kwargs).
將此行self.Bind(*args, **kwargs)
更改為:
super(MyBitmapButton, self).Bind(*args, **kwargs)
在python3中,super無需參數即可工作:
super().Bind(*args, **kwargs)
來自super
文檔 :
返回將方法調用委托給類型的父級或同級類的代理對象。 這對於訪問已在類中重寫的繼承方法很有用。 搜索順序與getattr()使用的順序相同,只是類型本身被跳過。
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.