Introduction

This recipe shows how to program responsive buttons. Such buttons change, when the mouse pointer enters or leaves the area of the button widget. In this case a button widget changes it's background colour.

What Objects are Involved

Process Overview

When you enter the area of the button widget with a mouse pointer, EVT_ENTER_WINDOW event is generated. Simirarly, EVT_LEAVE_WINDOW event is generated, when you leave the area of the widget. So all you have to do is to bind those events to functions, that will change the colour/shape of the button widget appropriately.

Code Sample


import wx
import wx.lib.buttons as buttons
from wxPython.lib import colourdb

class MyPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.btn = buttons.GenButton(self, -1, "Button", pos=(100,100))
        self.btn.SetBezelWidth(1)
        self.btn.SetBackgroundColour("DARKGREY")

        self.btn.Bind(wx.EVT_ENTER_WINDOW, self.EnterButton)
        self.btn.Bind(wx.EVT_LEAVE_WINDOW, self.LeaveButton)

    def EnterButton(self, event):
        """Move mouse into the button area."""
        self.btn.SetBackgroundColour("GREY79")
        self.btn.Refresh()

    def LeaveButton(self, event):
        """Leave mouse from the button area."""
        self.btn.SetBackgroundColour("DARKGREY")
        self.btn.Refresh()

class MyApp(wx.App):
    def OnInit(self):
        colourdb.updateColourDB()
        frame = wx.Frame(None, title="GenButton", size=(300,300))
        panel = MyPanel(frame)
        frame.Centre()
        frame.Show(True)
        return True

app = MyApp(0)
app.MainLoop()

ResponsiveButton (last edited 2008-03-11 10:50:39 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.