Re: Combining events?

Michael Lange <[email protected]>
Newsgroups gmane.comp.python.tkinter
Message-ID <[email protected]>
Hi,

On Fri, 18 Jul 2014 16:54:34 -0600
Bob Greschke <[email protected]> wrote:

> I have a huge program...several huge programs...and I keep writing
> duplicate sets of calls for <Return> and <KP_enter>.  I'm tired of it,
> even though they are already all written.  Is there any way to combine
> those (those two, specifically) into one bind like
> 
> x.bind(("<Return>", "<KP_Enter"), command = .....?
> 
> I know you can't do that, but something like that on a global scale
> (both figuratively, and programmatically).  Some little line of code at
> the beginning of the program that redirects the <KP_Enter> to the
> <Return> event when some field or whatever is specifically looking for
> either return key to be pressed?.

you could use event_generate() and bind_class(), as in this example:

from Tkinter import *
root = Tk()

def on_kp_enter(event):
    event.widget.event_generate('<Return>')
root.bind_class('Button', '<KP_Enter>', on_kp_enter)

def on_return(event):
    print('Return')

b = Button(root, text='Push me')
b.pack()
b.bind('<Return>', on_return)
root.mainloop()

Regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Lots of people drink from the wrong bottle sometimes.
		-- Edith Keeler, "The City on the Edge of Forever",
		   stardate unknown
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.