[Tkinter-discuss] Button look

Jeff Epler jepler at unpythonic.net
Wed Jun 9 18:14:31 EDT 2004


On Wed, Jun 09, 2004 at 03:39:44PM -0300, Batista, Facundo wrote:
> I want the first button to be triggered with the Enter key. So I binded the
> <Return> key to the same function that the button.
> 
> It works but I also want that the button look "pressed" when I press the key
> and the function starts, and look "unpressed" when the function finishes.

Use this button_invoker() in the binding.  Here I bound the button
widget, but on re-reading your text it sounds like you want to place
this binding on the toplevel containing the button, so that hitting
Enter anywhere in that window triggers the button.

import time
from Tkinter import Button

def button_invoker(event):
    w = event.widget
    old_relief = w.cget('relief')
    w.configure(relief='sunken')
    w.update_idletasks()
    try:
        w.invoke()
    finally:
        w.configure(relief=old_relief)

b = Button(command = lambda: time.sleep(5))
b.bind("<Return>", button_invoker)
b.pack()
b.focus()
b.mainloop()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tkinter-discuss/attachments/20040609/dfc41e4c/attachment.bin


More information about the Tkinter-discuss mailing list