wxPythons 超级方法和子类化小部件(如按钮)有什么区别



有人可以解释一下使用 wxPython 小部件子类与 super() 的区别/优势吗?我正在尝试创建一个没有封闭边框矩形的切换按钮,我想使用 wx.lib.statbmp 模块中的 GenStaticBitmap 作为基类。

我正在尝试将三个位图传递给子类,但在使这两种方法都不起作用方面取得了成功。我不确定如何构造子类并传递参数或使用 super 来创建类。

(我有 30 多个按钮要创建,这就是我尝试对控件进行子类化的原因)

当作为GenStaticBitmap放置在面板中时,该代码有效感谢您的任何帮助或建议。

这是我尝试过的:

class MyBitmapButton(gen_statbmp.GenStaticBitmap):
    """ This is the subclassed version of the GenStaticBitmap to allow embedding bitmaps in the button"""
    def __init__(self, parent, off_bmp,on_bmp,hover_bmp):
        gen_statbmp.GenStaticBitmap.__init__(parent,id,off_bmp)
        self.bmapoff = off_bmp
        self.bmapon = on_bmp
        self.bmaphover= over_bmp
        self.state='off'
    def On_Hover(self,event):
        self.SetBitmap(self.bmphover)
        print 'on hover button state is',self.state
    def On_Leave(self,event):       
        #print 'on enter button state is',self.state
        if self.state=='on':
            print self.state
            self.SetBitmap(self.bmapon)
            self.state='on'
        if self.state=='off':
            self.SetBitmap(self.bmapoff)
            self.state='off'
        print 'on leave button state is',self.state
    def On_Click(self,event):
        #print 'on enter button state is',self.state
        if self.state=='on':
            print self.bmapbtn.state
            self.SetBitmap(self.bmapoff)
            self.state='off'
        elif self.state=='off':
            self.SetBitmap(self.bmapon)
            self.state='on'
        print 'on leave button state is',self.state

wxPython 存档解释了整个"超级"意识形态:

http://wxpython-users.1045709.n5.nabble.com/Super-object-usage-explanation-td3408498.html

如果您尝试创建自定义小部件,则应查看 wx.lib 小部件。我认为它们都是自定义小部件。

另请参阅:

  • http://wiki.wxpython.org/CreatingCustomControls
  • http://zetcode.com/wxpython/customwidgets/

最新更新