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

Michael Lange klappnase at web.de
Thu Oct 11 19:19:52 CEST 2012


Hi,

On Thu, 11 Oct 2012 18:20:10 +0200
mkieverpy at tlink.de wrote:

>  My main interest is capturing all keyboard input
>  with these lines:
>  ------------------------------
>  def on_key(event):
>      print('key')
> 
>  f.bind('<Key>', on_key)
>  ------------------------------
>  I want to use this to implement keyboard control for some features.
>  In the case where 'f' has children which can get focus
>  I may loose keyboard input because 'f' won't get notified
>  about FocusOut (lines 2-4 in my above description).
>  In the workaround I use a frame without children.
>  In this case I always get the FocusOut event and
>  can reclaim focus with 'f.focus_set()'.

I am not sure if I understand correctly what you are trying to achieve;
in one my programs there is a window with a couple of keyboard bindings
for this and that, there I simply put all the bindings into one method
like this:

    def apply_default_bindings(self, widget):
        for key in ('KP_Home', 'KP_End', 'KP_Insert', 'KP_Up', 'KP_Left',
                    'KP_Right', 'KP_Down', 'KP_Next', 'KP_Prior',
                    'KP_Begin', 'z'):
             widget.bind('<Control-%s>' % key, self.zoom_in_by_key)
        for key in ('KP_0', 'KP_1', 'KP_2', 'KP_3', 'KP_4', 'KP_5',
                    'KP_6', 'KP_7', 'KP_8', 'KP_9', 'Z'):
            widget.bind('<Control-Shift-%s>' %key, self.zoom_out_by_key)
        for key in ('Left', 'Right', 'Prior', 'Next', 'Home', 'End'):
            widget.bind('<%s>' % key, self.scroll_canvas)
        widget.bind('<Control-o>', self.openfile)
        widget.bind('<Control-s>', self.stop)
        widget.bind('<Control-p>', self.play)
        widget.bind('<Control-i>', self.pause)
        widget.bind('<Control-Down>', self.set_volume)
        widget.bind('<Control-Up>', self.set_volume)
        widget.bind('<Control-n>', self.setmark)
        widget.bind('<Control-d>', self.clearmarks)
        widget.bind('<Control-t>', self.see_next_mark)
        widget.bind('<Control-Shift-T>', self.see_prev_mark)
        widget.bind('<Control-c>', self.see_cursor)
        widget.bind('<Control-Shift-C>', self.center_cursor)
        widget.bind('<Control-Left>', self.move_cursor)
        widget.bind('<Control-Right>', self.move_cursor)
        widget.bind('<Control-Prior>', self.move_cursor)
        widget.bind('<Control-Next>', self.move_cursor)

and call this once every widget in the window, which
costs a few extra lines, but otoh saves quite some headaches about which
widget is focussed.

Regards

Michael


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

Leave bigotry in your quarters; there's no room for it on the bridge.
		-- Kirk, "Balance of Terror", stardate 1709.2


More information about the Tkinter-discuss mailing list