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

Michael Lange klappnase at web.de
Wed Oct 10 11:37:35 CEST 2012


Hi,

On Mon, 08 Oct 2012 11:09:30 -0000
mkieverpy at tlink.de wrote:

(...)
> 
> The other program does not look wrong though.
> I think I'll ask at tcl/tk if they think it's a bug.

The problem in your first example is the following:

def on_focus_out(event):
    print('focus out')
    f.focus_set()
f.focus_set()
f.bind('<Key>', on_key)
f.bind('<FocusOut>', on_focus_out)

Now you force the focus when it leaves f immediately back to f - and this
is exactly what Tk does : when you hit "Tab" while the "foo" button is
focussed the <FocusOut> event for f is triggered and so the focus is
moved to f again (but *not* to the button and thus not visibly
highlighted). Then you hit "tab" again and the focus moves to the
"foo"-button again, but does not leave the parent-Frame f and so no
<FocusOut> event is created. I changed your example a little to help see
what's going on:

import tkinter

root = tkinter.Tk()
f = tkinter.Frame(root)
f.pack(fill='both', expand=1)
tkinter.Button(f, text='foo').pack()

f2 = tkinter.Frame(root)
f2.pack(fill='both')
tkinter.Button(f2, text='bar').pack()

def on_key(event):
    print('key')
def on_focus_out(event):
    print('focus out')
    f.focus_set()
f.focus_set()
f.bind('<Key>', on_key)
f.bind('<FocusOut>', on_focus_out)

def on_focus_in(event):
    print('focus in\n')
f.bind('<FocusIn>', on_focus_in)

root.mainloop()


On Sun, 07 Oct 2012 18:53:21 -0000
mkieverpy at tlink.de wrote:

> The problem is when I use the 'Tab' key to move
> the focus. The first 'Tab' moves the focus
> to the 'foo' button. 'f' no longer gets key events
> but does not get the focus out event either.
> The next 'Tab' moves the focus to the 'bar' button,
> 'f' gets the focus out event and key events arrive again
> after I do 'focus_set'.
> So it looks like tk standard behaviour. Can I get what I want?

With your example from the previous post the focus will never reach the
"bar" button, maybe it was a typo in on_focus_out() and should be
f2.focus.set() ?
Anyway, I am not sure what exactly you want to achieve (maybe I missed
something from your previous posts?), is there a particular reason why not
to do nothing about focus handling and just leave it to the WM?


Regards

Michael


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

We have found all life forms in the galaxy are capable of superior
development.
		-- Kirk, "The Gamesters of Triskelion", stardate 3211.7


More information about the Tkinter-discuss mailing list