Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > New Tkinter windows don't get focus on OS X

Reply
Thread Tools

New Tkinter windows don't get focus on OS X

 
 
Joshua Bronson
Guest
Posts: n/a
 
      09-10-2009
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
 
Reply With Quote
 
 
 
 
Kevin Walzer
Guest
Posts: n/a
 
      09-10-2009
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. The
Command-Tab behavior is determined by the windowing system, not by a
newly spawned process.

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.

Kevin



--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
 
Reply With Quote
 
 
 
 
Joshua Bronson
Guest
Posts: n/a
 
      09-10-2009
Hey Kevin,

Thanks for your quick reply.

On Sep 10, 10:12*am, Kevin Walzer <k...@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

 
Reply With Quote
 
Kevin Walzer
Guest
Posts: n/a
 
      09-10-2009
Hi Joshua,

> 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.


OK...

>
> 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?


I've confirmed this with wx...not sure why Tk behaves differently.

>
>> 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.


I'm not sure there's a way to do this.

--Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      09-11-2009
On Thursday 10 September 2009 18:19:09 Joshua Bronson wrote:

> 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.
>


look for widget.focus_force()
and look for widget.grab_set_global()

Yahoo for: Shipman Tkinter "new mexico tech"
for a nice manual, if you do not have it yet.

HTH - Hendrik


 
Reply With Quote
 
eb303
Guest
Posts: n/a
 
      09-11-2009
On Sep 11, 9:14 am, Hendrik van Rooyen <hend...@microcorp.co.za>
wrote:
> On Thursday 10 September 2009 18:19:09 Joshua Bronson wrote:
>
> > 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.

>
> look for widget.focus_force()
> and look for widget.grab_set_global()


Doesn't work. BTW, forcing the focus or setting the grab globally are
usually considered very annoying and I don't know any windowing system
or window manager honouring those.

For the OP: the problem comes from the tcl/tk level. Running a tcl
script just opening a window from the terminal shows the same
behaviour. You might want to forward the question to the tcl guys.
 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      09-11-2009
On Friday 11 September 2009 09:53:56 eb303 wrote:
> On Sep 11, 9:14 am, Hendrik van Rooyen <hend...@microcorp.co.za>
> wrote:


> > look for widget.focus_force()
> > and look for widget.grab_set_global()

>
> Doesn't work. BTW, forcing the focus or setting the grab globally are
> usually considered very annoying and I don't know any windowing system
> or window manager honouring those.


I have to confess I have never used the stuff - just remembered seeing it in
the manual and pointed it out.

What does it do?

- Hendrik

 
Reply With Quote
 
eb303
Guest
Posts: n/a
 
      09-11-2009
On Sep 11, 10:40 am, Hendrik van Rooyen <hend...@microcorp.co.za>
wrote:
> On Friday 11 September 2009 09:53:56 eb303 wrote:
>
> > On Sep 11, 9:14 am, Hendrik van Rooyen <hend...@microcorp.co.za>
> > wrote:
> > > look for widget.focus_force()
> > > and look for widget.grab_set_global()

>
> > Doesn't work. BTW, forcing the focus or setting the grab globally are
> > usually considered very annoying and I don't know any windowing system
> > or window manager honouring those.

>
> I have to confess I have never used the stuff - just remembered seeing it in
> the manual and pointed it out.
>
> What does it do?


At tcl level, focus_force() and grab_set_global() are translated as
options to the equivalents of focus_set() and grab_set() [1][2]. If
these are not honoured, the options are simply ignored. That's what
seems to happen on Mac OS X. So focus_force() does the same as
focus_set(), and grab_set_global() the same as grab_set(). No use for
the OP's problem...

[1] http://www.tcl.tk/man/tcl8.5/TkCmd/focus.htm
[2] http://www.tcl.tk/man/tcl8.5/TkCmd/grab.htm
 
Reply With Quote
 
Joshua Bronson
Guest
Posts: n/a
 
      09-15-2009
On Sep 11, 3:53*am, eb303 <eric.bru...@pragmadev.com> wrote:
> For the OP: the problem comes from the tcl/tk level. Running a tcl
> script just opening a window from the terminal shows the same
> behaviour. You might want to forward the question to the tcl guys.


Done:
https://sourceforge.net/mailarchive/...m_name=tcl-mac
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the differences between tkinter in windows and Tkinter inthe other platform? Hidekazu IWAKI Python 0 12-15-2009 05:58 AM
What is the differences between tkinter in windows and Tkinter in theother platform? Hidekazu IWAKI Python 1 12-14-2009 03:44 PM
Keeping focus with sequential Tkinter windows? William McBrine Python 0 05-28-2008 05:01 AM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
any manual focus cameras easy to focus? Catherine Jo Morgan Digital Photography 6 11-05-2003 06:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57