Welcome, guest | Sign In | My Account | Store | Cart

Simple Button widget with an extra option added on that changes the color of the fg when the mouse cursor is over it, and then back whent he mouse cursor leaves. Just makes the buttons look nicer.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
    Button Widget to be used with Tkinter, that allows
    you to make a button that changes fg color when
    the mouse is over it.
"""
import Tkinter

class ColorButton(Tkinter.Widget):
    """
    Slightly different Button widget, with an option added: OnColor:
    This will change the fg of the Button to that color when the mouse
    is over it. Default OnColor is 'red'"""
    
    def __init__(self, master, OnColor = 'red', cnf = {}, **kw):
        Tkinter.Widget.__init__(self, master, 'button', cnf, kw)
        if not 'fg' in kw:
            kw['fg'] = 'black'
        self.bind("<Enter>", lambda Event:self.Enter(OnColor))
        self.bind("<Leave>", lambda Event:self.Leave(kw['fg']))

    def Enter(self, Color):
        """Internal Function"""
        
        self['fg'] = Color

    def Leave(self, Color):
        """Internal Function"""
        
        self['fg'] = Color
Created by Patrick Cossette on Sun, 30 Dec 2007 (PSF)
Python recipes (4591)
Patrick Cossette's recipes (2)

Required Modules

Other Information and Tasks