classification
Title: Original window focus when opening IDLE by double clicking Python file Mac
Type: behavior Stage:
Components: IDLE, macOS Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: IrvKalb, ned.deily, ronaldoussoren, terry.reedy
Priority: normal Keywords:

Created on 2020-01-14 01:24 by IrvKalb, last changed 2020-06-05 02:37 by terry.reedy.

Messages (5)
msg359942 - (view) Author: Irv Kalb (IrvKalb) Date: 2020-01-14 01:24
I have my Mac to open ".py" files with IDLE.  If IDLE is not running, and I double click on a Python file, the Shell window opens, then the Python file I clicked on opens in front, but the Shell has keyboard focus.  In order to edit or run the source file, I must click in that window to give it focus.

I have made a two minute video that is available here as a private video:

https://youtu.be/Fs_ZAiej-WI

Thanks for your consideration.

Irv
msg365181 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-03-27 18:53
Ned, I believe that this issue results from peculiarities of how macOS runs apps.  Insight from you would be appreciated.

macOS Sierra 10.2.8?, Python 3.7.3 (Did you use python.org installer?)

On my Win10, multiple IDLE windows are cascaded to the right and down.  Each window opened gets the focus until the focus is moved.  So on startup, the last window opened ends up with the focus.
  'python -m idlelib tem tem2' opens tem and then tem2.
  'python -m idlelib -i tem' opens tem then a shell.

The order is set in pyshell.main(), where sys.args is parsed to set boolean enable_edit and enable_shell.  Then 'if enable_edit: ...' and 'if enable_shell: ...' execute.  I presume that the logic is that if one wants to immediately edit and run an existing file, there is little reason pass args to cause a shell opened also.

I watched your video a couple of times.  It appears to show Shell being opened first (with the focus) and then an editor being opened on top of it, but without getting the focus.  AFAIK, both are impossible.

One possible interpretation: editor and shell are opened in the normal order, with the editor somehow not appearing.  Then macOS intervenes to lift the editor above the shell, but without shifting the focus.  Or macOS intervenes some other way.  Or the specific tk in use on macOS has some undocumented macOS-specific behavior.  (The tcl/tk folk claim that 8.6.10 follows the doc better.)  In all these cases, IDLE's code is not responsible.  If you want to chase down the details, add some logging to pyshell.main.

The root problem is asking IDLE to open a shell.  For me, 'python -m idlelib tem' only opens tem without a shell.  Ditto for right-click 'Edit with IDLE'.  Can you tell what command is used to start IDLE with double clicks?  Can you change it to not open Shell?  Also, does updating to 3.7.7 (recommended) change anything?

I am inclined to close this as a 3rd party or non-bug issue, but will wait a bit for Ned to comment.
msg365185 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-03-27 19:17
Double clicking in finder with current Mohave (10.14.6?) does same as described here.  See Ned's comment (msg357667) for #38946.
msg365188 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-03-27 19:23
In mac Terminal, 'python3 -m idlelib tem.py' opens tem.py without shell, just as on Windows.
msg370747 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-06-05 02:37
Irv also posted to idledev, where he explained his particular need: He teaches a class with assignment to write a .py file with certain behavior.  Submissions are in 1 directory.  He would like to double-click to load a file and F5 to run it.  Currently, he has to move the mouse and click the loaded editor before running and move it back.

In reply to my post yesterday, he explained how to make double-click in finder open in IDLE: """I did change the default for how my Mac opens ".py" files.  To do that I selected any ".py" and did a Get Info on it.  A tall window opens, and allows me to change the default application for opening the file.  I selected IDLE.app and chose "Change all"."""

More testing with my Macbook: both 'python3.9 -m idlelib -i file.py' in terminal *and* double-clicking in Finder open an editor, with 'Format' in the IDLE menu, open Shell (shifted right), with 'Shell' in the IDLE menu, and then lift the editor window, without giving it focus and changing the IDLE menu back.  Rereview of the video (which omits the IDLE menu) suggests the same because Shell is to the right of the editor.  The video is not as clear because the IDLE menu is omitted, the  editor has only one line, and Irv's machine is much faster.

I consider the inconsistency to be a bug.  The cause is this code after the creation of Shell.

        if macosx.isAquaTk() and flist.dict:
            # On OSX: when the user has double-clicked on a file that causes
            # IDLE to be launched the shell window will open just in front of
            # the file she wants to see. Lower the interpreter window when
            # there are open files.
            shell.top.lower()

Widgit.raise() and .lower() do not change focus.  Since there is no way to shed focus, we need to save the focus window, if there is one (and make sure it is not the withdrawn root), before creating Shell and restore it after.  There might be situations in which Shell should be left on top with the focus.
History
Date User Action Args
2020-06-05 02:37:17terry.reedysetnosy: + ronaldoussoren
messages: + msg370747
components: + macOS
2020-03-27 19:23:20terry.reedysetmessages: + msg365188
2020-03-27 19:17:18terry.reedysetmessages: + msg365185
2020-03-27 18:53:19terry.reedysetnosy: + ned.deily
messages: + msg365181
2020-01-14 01:24:01IrvKalbcreate