newbie - tkinter - how to do button rollover

John Grayson johngrayson at
Thu May 18 12:57:15 CEST 2000

In article <8fva74$caa$1 at>,
  leapinglemur00 at wrote:
> I seem to be missing something -- I'd like to do
> a "rollover" sort of effect with the image on a
> button changing as the mouse goes over it.  I
> know how to respond to the mouse event, but how
> do you change the image?  Is there a method on
> the Button class to set the image?  Is there some
> place in the documentation where I could find
> this?
> Thanks a lot...

Here is a minimal example. Choose your own images...

from Tkinter import *

class ActiveButton(Button):
    def __init__(self, master, inimage=None, outimage=None,
	self.master = master
	self.inI  = PhotoImage(file='icons/%s' % inimage)
	self.outI = PhotoImage(file='icons/%s' % outimage)
        Button.__init__(self, master, command=activate, image=self.outI)

	self.bind('<Any-Enter>', lambda e, state=1, s=self:
                                          s.change(e, state))
	self.bind('<Any-Leave>', lambda e, state=0, s=self:
                                          s.change(e, state))

    def change(self, event, state):
	if state:

if __name__ == '__main__':
    root = Tk()
    button = ActiveButton(root, inimage='smooth.gif',

Sent via
Before you buy.

More information about the Python-list mailing list