newbie - tkinter - how to do button rollover

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


In article <8fva74$caa$1 at nnrp1.deja.com>,
  leapinglemur00 at my-deja.com 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,
		 activate=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:
	    self.configure(image=self.inI)
	else:
	    self.configure(image=self.outI)

if __name__ == '__main__':
    root = Tk()
    button = ActiveButton(root, inimage='smooth.gif',
                          outimage='tline1.gif',
		          activate=root.destroy)
    button.pack()
    root.mainloop()



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list