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

# interactivebutton.py

import wx
from wx.lib.buttons import GenButton

class MyFrame(wx.Panel):
    def __init__(self, parent, ID):
        wx.Panel.__init__(self, parent, ID, wx.DefaultPosition)

        self.btn = GenButton(self, -1, "button", pos = wx.Point(125,100), size=(-1, -1))
        self.btn.SetBezelWidth(1)
        self.btn.SetBackgroundColour( "DARKGREY")

        wx.EVT_ENTER_WINDOW(self.btn, self.func)
        wx.EVT_LEAVE_WINDOW(self.btn, self.func1)

    def func(self, event):
        self.btn.SetBackgroundColour( "GREY79")
        self.btn.Refresh()

    def func1(self, event):
        self.btn.SetBackgroundColour( "DARKGREY")
        self.btn.Refresh()

class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(None, -1, "interactivebutton.py", wx.DefaultPosition, wx.Size(350,300))
        myframe = MyFrame(frame, -1)
        frame.Centre()
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()

InteractiveButton (last edited 2008-07-10 18:43:04 by 65)

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