New Tkinter windows don't get focus on OS X

Joshua Bronson jabronson at gmail.com
Thu Sep 10 12:19:09 EDT 2009


Hey Kevin,

Thanks for your quick reply.

On Sep 10, 10:12 am, Kevin Walzer <k... at codebykevin.com> wrote:
> On 9/10/09 1:27 AM, Joshua Bronson wrote:
> > If you try something like:
>
> > $ python -m Tkinter -c 'Tkinter._test()'
>
> > in Terminal on OS X, you'll notice that the window that is spawned
> > does not get focus, rather focus remains in Terminal. Furthermore, if
> > you hit Command+Tab to switch focus to the Python process, you'll
> > notice that for some reason Python has been placed last in the focus
> > order, even though it was just spawned by Terminal. This is
> > undesirable if you're e.g. making a game where stuff happens as soon
> > as the window is spawned and the player is expected to respond quickly
> > but can't until she Command+Tabs over or clicks in the unfocused
> > window. I've googled for this extensively and haven't found a way
> > around it, so I thought I'd ask here. If there is a more appropriate
> > place to ask, please let me know.
>
> > Thanks,
> > Josh
>
> Yes, this is standard  behavior for OS X. Running an app in Terminal
> keeps focus in the Terminal unless you switch by clicking windows.

At first I was puzzled by this, because if you run something like
"open -a TextEdit", TextEdit gets the focus. But then I realized this
must be because of the "open" command. Indeed, if you try running
something like "/Applications/MacPorts/Python\ 2.6/IDLE.app/Contents/
MacOS/IDLE" focus will remain in Terminal.

On the other hand, every other GUI toolkit I've used (e.g. wxPython,
PyQt4, pyglet) does not have this problem. (If you try the basic
example from http://www.wxpython.org/tut-part1.php you'll see what I
mean.) How are these other toolkits able to steal the focus? More
important, is it *possible* to have a Tkinter app steal the focus,
even if it's not the default behavior?

> The Command-Tab behavior is determined by the windowing system, not
> by a newly spawned process.

Sure, but I've always noticed a correspondence between the focus order
and the order in which an application was spawned. So the conclusion
is that there's just a system-wide inconsistency here in the case of
spawning GUI apps from Terminal.

> The way around this is to wrap your application up in a standard Mac app
> bundle using py2app. The average Mac user isn't going to launch a
> Python-based game from the command line.

True, but it'll still be a lot less painful for me to test my app if I
can get it to steal focus
when launched from the command line. If anyone knows how to do this in
Tkinter, help would be much appreciated.

Thanks,
Josh

> Kevin
>
> --
> Kevin Walzer



More information about the Python-list mailing list