[Tkinter-discuss] FocusOut not working as expected - tix broken?

Bryan Oakley bryan.oakley at gmail.com
Thu Oct 11 19:35:23 CEST 2012

On Thu, Oct 11, 2012 at 11:20 AM,  <mkieverpy at tlink.de> wrote:
> Sorry, to have been unclear here.
> My main interest is capturing all keyboard input
> with these lines:
> ------------------------------
> def on_key(event):
>     print('key')
> f.bind('<Key>', on_key)

There are better ways to capture all keyboard input if that's your
real goal. Are you aware of the "bind_all" method, which associates a
binding with all widgets? Typically this is used for shortcuts, but it
can be used any time you want to capture certain events no matter
where they occur.

Tkinter has the notion of "bind tags" (or bindtags). To this day, no
other GUI toolkit has as powerful of a mechanism for managing events;
this is truly one of Tkinter's hidden gems. The short version is, you
can associate a custom tag to any (or every) widget, and then bind to
that tag. Then, no matter what widget has focus, your binding will
fire. The advantage this has over "bind_all" is that with the bindtag
you can control when the event is processed -- before the actual
event, after the actual event, etc. With "bind_all", the "all"
bindings by default are processed after event and class level
bindings. The practical implication of that is that the widget or
class bindings have the option to prevent the event from propagating.
With bind tags you can guarantee your bindings fire before any others.


More information about the Tkinter-discuss mailing list