This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Idle: always start with focus
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: markroseman, python-dev, serhiy.storchaka, terry.reedy
Priority: high Keywords: patch

Created on 2015-08-18 20:03 by terry.reedy, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
idle-focus-fix.diff terry.reedy, 2015-08-18 20:03 add focus_force review
Messages (4)
msg248790 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-08-18 20:03
On Windows, starting Idle with an icon or at console brings up shell or editor window with focus, ready to receive input. Starting Idle from a built python_d in interactive console mode with '>>> import idlelib.idle' brings up an Idle shell with a cursor blinking at the '>>> ' prompt, but leaves the input focus with the console until one clicks on the Shell window.  This is quite annoying and I want to fix it.

I have the impression that this may be a Windows-only problem.  True?

PyShell.main calls PyShellFileList.open_shell, which calls PyShell.begin. I believe this is the only place .begin is called, and it is only called when there is no existing Shell.

Calling text.focus_force() works for me.  .focus_set does not as the issue is application focus rather than widget focus within Idle.  Does this or might this cause any problems with Linux or Mac?

--
Side note: any comment on this line in the context?
         tkinter._default_root = None # 03Jan04 KBK What's this?
msg248857 - (view) Author: Mark Roseman (markroseman) * Date: 2015-08-19 22:09
I reproduced your problem on Windows, and verified that it wasn't an issue on either Mac or Linux.

Your fix however, didn't change anything for me (on a Windows 7 VM). I tried a few other things (raising the window, playing with various wm attributes, etc.) with no luck.

The fact that it works ok when run directly from the command prompt got me thinking, and I eventually tracked down what is preventing IDLE from becoming the front application.

It's actually an internal Windows feature, that's designed to prevent background applications from all of a sudden putting up windows that interfere with what you're doing in the foreground, instead putting a notification in the task bar and making you click that or the window itself.

The mechanism used is to prevent applications from coming to the front on their own if they don't do so very shortly after they're first launched. So when you launch a Python shell (e.g. from a command prompt), the "delay" it takes you to type "import idlelib.idle" plus the launch time exceeds this timeout, and Windows decides it won't allow it to put the window in the foreground.

You can change the timeout (or set it to 0 to disable the feature) via the registry key HKEY_CURRENT_USER\Control Panel\Desktop\ForegroundLockTimeout
(you need to restart after). With that change, your fix worked for me.

You can Google around for "ForegroundLockTimeout" for additional info.

There's not realistically an easy way to get around it in IDLE itself (for obvious reasons, given the purpose of the feature).

I'm guessing this will affect few people as much as it does you, constantly starting and stopping IDLE from within Python. If the fix works on your system, especially without mucking with that registry key, that should cover it. The fix doesn't cause any harm on Mac or Linux.
msg250076 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-09-07 05:59
New changeset 741b033c5290 by Terry Jan Reedy in branch '2.7':
Issue #24889: When starting Idle, force focus onto Idle window if not already
https://hg.python.org/cpython/rev/741b033c5290

New changeset d7449bac2c6d by Terry Jan Reedy in branch '3.4':
Issue #24889: When starting Idle, force focus onto Idle window if not already
https://hg.python.org/cpython/rev/d7449bac2c6d
msg250077 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-09-07 06:02
Thanks for the research and verification that this is harmless. It has been a daily annoyance.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 69077
2015-09-07 06:02:24terry.reedysetstatus: open -> closed
resolution: fixed
messages: + msg250077

stage: commit review -> resolved
2015-09-07 05:59:42python-devsetnosy: + python-dev
messages: + msg250076
2015-08-19 22:09:47markrosemansetmessages: + msg248857
2015-08-18 20:03:11terry.reedycreate