代码之家  ›  专栏  ›  技术社区  ›  Wallter

python-wxpython自定义按钮->未绑定方法uu init_uuuuu()?什么?

  •  0
  • Wallter  · 技术社区  · 16 年前

    在看了以下问题之后 this 我的 __init__(self, parrent, id) 会引发未绑定的错误吗?帮助?

    Me.Py

    import wx
    from customButton import customButton
    from wxPython.wx import *
    
    class MyFrame(wx.Frame):
        def __init__(self, parent, ID, title):
            wxFrame.__init__(self, parent, ID, title,
                             wxDefaultPosition, wxSize(400, 400))
    
    # Non-important code here...
    
    # This is the first declaration of the Button1
    # This is also where the ERROR is thrown.
    # Omitting this line causes the window to execute
    # flawlessly.
    self.Button1 = customButton.__init__(self, parent, -1)
    
    # ... finishes in a basic wx.program style...
    

    定制按钮

    # I've included all of the code in the file
    # because have no idea where the bug/error happens
    
    import wx
    from wxPython.wx import *
    
    class Custom_Button(wx.PyControl):
    
                                        # The BMP's
        Over_bmp = None #wxEmptyBitmap(1,1,1)         # When the mouse is over
        Norm_bmp = None #wxEmptyBitmap(1,1,1)         # The normal BMP
        Push_bmp = None #wxEmptyBitmap(1,1,1)         # The down BMP
    
        def __init__(self, parent, id, **kwargs):
            wx.PyControl.__init__(self,parent, id, **kwargs)
    
    
    
    
            # Set the BMP's to the ones given in the constructor
            #self.Over_bmp = wx.Bitmap(wx.Image(MOUSE_OVER_BMP, wx.BITMAP_TYPE_ANY).ConvertToBitmap())
            #self.Norm_bmp = wx.Bitmap(wx.Image(NORM_BMP, wx.BITMAP_TYPE_ANY).ConvertToBitmap())
            #self.Push_bmp = wx.Bitmap(wx.Image(PUSH_BMP, wx.BITMAP_TYPE_ANY).ConvertToBitmap())
            #self.Pos_bmp = self.pos
    
            self.Bind(wx.EVT_LEFT_DOWN, self._onMouseDown)
            self.Bind(wx.EVT_LEFT_UP, self._onMouseUp)
            self.Bind(wx.EVT_LEAVE_WINDOW, self._onMouseLeave)
            self.Bind(wx.EVT_ENTER_WINDOW, self._onMouseEnter)
            self.Bind(wx.EVT_ERASE_BACKGROUND,self._onEraseBackground)
            self.Bind(wx.EVT_PAINT,self._onPaint)
    
            self._mouseIn = self._mouseDown = False
    
        def _onMouseEnter(self, event):
            self._mouseIn = True
    
        def _onMouseLeave(self, event):
            self._mouseIn = False
    
        def _onMouseDown(self, event):
            self._mouseDown = True
    
        def _onMouseUp(self, event):
            self._mouseDown = False
            self.sendButtonEvent()
    
        def sendButtonEvent(self):
            event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
            event.SetInt(0)
            event.SetEventObject(self)
            self.GetEventHandler().ProcessEvent(event)
    
        def _onEraseBackground(self,event):
            # reduce flicker
            pass
    
        def _onPaint(self, event):
            dc = wx.BufferedPaintDC(self)
            dc.SetFont(self.GetFont())
            dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
            dc.Clear()
            dc.DrawBitmap(self.Norm_bmp)
    
            # draw whatever you want to draw
            # draw glossy bitmaps e.g. dc.DrawBitmap
            if self._mouseIn:   # If the Mouse is over the button
                dc.DrawBitmap(self, self.Mouse_over_bmp, self.Pos_bmp, useMask=False)
            if self._mouseDown: # If the Mouse clicks the button
                dc.DrawBitmap(self, self.Push_bmp, self.Pos_bmp, useMask=False)
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   Ned Batchelder    16 年前

    您不会创建这样的对象:

    self.Button1 = customButton.__init__(self, parent, -1)
    

    你这样做:

    self.Button1 = customButton(parent, -1)
    

    __init__ 是在对象创建期间隐式调用的方法。

        2
  •  1
  •   Ignacio Vazquez-Abrams    16 年前

    不要打电话 __init__() 除非你知道你需要。

    self.Button1 = customButton(parent, -1)