focus_set() doesn't work

Eric Brunel eric.brunel at pragmadev.com
Fri Oct 11 12:43:00 EDT 2002


Heiner Litz wrote:
> HI
> 
> why does my new Toplevel widget:
> 
> tk=Tkinter()

(assuming Tkinter.Tk() here...)

> tk.focus_set()
> 
> does not get the focus ?? *grr*
> 
> the Top Line of the window remains grey instead of blue, like it should
> be.

You step in a bit of a mess here: different window managers (not to mention 
different OSs) manage the input focus quite differently. So you'll have to 
fiddle with focus_set, focus_force, tkraise, and so on to achieve the 
result you want.

I tried your code on a Linux box with KDE 2 and it actually doesn't work. 
However, using focus_force instead of focus_set did the trick. The 
difference is subtle: focus_set gives the focus to the window if your 
application already had the focus (note that it's not the case when you 
issue interactive commands, since the application having the focus is then 
the terminal running the Python interpreter), and focus_force gives the 
focus to your window even if your application didn't have the focus, which 
is considered evil in many cases (see Tk documentation @ 
http://www.tcl.tk/man/tcl8.3/TkCmd/focus.htm )

HTH. Good luck!
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list