开发者

Python - wxPython custom button -> unbound method __init__()? what?

开发者 https://www.devze.com 2023-01-03 10:12 出处:网络
After looking at questions like this it doesn\'t make sense that my __init__(self, parrent, id) would be throwing a unbound error? help?

After looking at questions like this it doesn't make sense that my __init__(self, parrent, id) would be throwing a unbound error? help?

main.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...

customButton.py

# 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.BIT开发者_JAVA技巧MAP_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)


You don't create an object like this:

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

you do it like this:

self.Button1 = customButton(parent, -1)

__init__ is an implicitly invoked method during object creation.


Don't call __init__() explicitly unless you know you need to.

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

精彩评论

暂无评论...
验证码 换一张
取 消