如何在wx中制作自定义按钮?

时间:2020-12-21 12:45:22

I'd like to make a custom button in wxPython. Where should I start, how should I do it?

我想在wxPython中创建一个自定义按钮。我应该从哪里开始,我应该怎么做?

4 个解决方案

#1


8  

Here is a skeleton which you can use to draw totally custom button, its up to your imagination how it looks or behaves

这是一个骨架,您可以使用它来绘制完全自定义按钮,它可以让您想象它的外观或行为

class MyButton(wx.PyControl):

    def __init__(self, parent, id, bmp, text, **kwargs):
        wx.PyControl.__init__(self,parent, id, **kwargs)

        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()
        # draw whatever you want to draw
        # draw glossy bitmaps e.g. dc.DrawBitmap
        if self._mouseIn:
            pass# on mouserover may be draw different bitmap
        if self._mouseDown:
            pass # draw different image text 

#2


5  

When I wanted to learn how to make custom widgets (buttons included) I referenced Andrea Gavana's page (full working example there) on the wxPyWiki and Cody Precord's platebutton (the source is in wx.lib.platebtn, also here in svn). Look at both of those and you should be able to build most any custom widget you would like.

当我想学习如何制作自定义小部件(包括按钮)时,我在wxPyWiki和Cody Precord的平板按钮上引用了Andrea Gavana的页面(完整的工作示例)(源代码在wx.lib.platebtn中,也在svn中)。看看这两个,你应该能够构建你想要的任何自定义小部件。

#3


3  

You can extend the default button class, like this for example:

您可以扩展默认按钮类,例如:

class RedButton(wx.Button):
    def __init__(self, *a, **k):
        wx.Button.__init__(self, *a, **k)
        self.SetBackgroundColour('RED')
        # more customization here

Every time you put a RedButton into your layout, it should appear red (haven't tested it though).

每次将RedButton放入布局时,它都应显示为红色(尽管尚未测试)。

#4


2  

Try using a Generic Button or a Bitmap Button.

尝试使用通用按钮或位图按钮。

#1


8  

Here is a skeleton which you can use to draw totally custom button, its up to your imagination how it looks or behaves

这是一个骨架,您可以使用它来绘制完全自定义按钮,它可以让您想象它的外观或行为

class MyButton(wx.PyControl):

    def __init__(self, parent, id, bmp, text, **kwargs):
        wx.PyControl.__init__(self,parent, id, **kwargs)

        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()
        # draw whatever you want to draw
        # draw glossy bitmaps e.g. dc.DrawBitmap
        if self._mouseIn:
            pass# on mouserover may be draw different bitmap
        if self._mouseDown:
            pass # draw different image text 

#2


5  

When I wanted to learn how to make custom widgets (buttons included) I referenced Andrea Gavana's page (full working example there) on the wxPyWiki and Cody Precord's platebutton (the source is in wx.lib.platebtn, also here in svn). Look at both of those and you should be able to build most any custom widget you would like.

当我想学习如何制作自定义小部件(包括按钮)时,我在wxPyWiki和Cody Precord的平板按钮上引用了Andrea Gavana的页面(完整的工作示例)(源代码在wx.lib.platebtn中,也在svn中)。看看这两个,你应该能够构建你想要的任何自定义小部件。

#3


3  

You can extend the default button class, like this for example:

您可以扩展默认按钮类,例如:

class RedButton(wx.Button):
    def __init__(self, *a, **k):
        wx.Button.__init__(self, *a, **k)
        self.SetBackgroundColour('RED')
        # more customization here

Every time you put a RedButton into your layout, it should appear red (haven't tested it though).

每次将RedButton放入布局时,它都应显示为红色(尽管尚未测试)。

#4


2  

Try using a Generic Button or a Bitmap Button.

尝试使用通用按钮或位图按钮。