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
wxGenButton - a special kind of a Button, that enables some further modifications. wxButton would work fine as well.
- colourdb - a colour database. This database enables us to use DARKGREY, GREY79 etc. colour names. For more inforation look at the demo.
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()