python / cpython

The Python programming language
https://www.python.org
Other
63.68k stars 30.51k forks source link

Space does not make `turtledemo/sorting_animate.py` Exit normally #125763

Open Wulian233 opened 1 month ago

Wulian233 commented 1 month ago

Bug report

Bug description:

Separate from https://github.com/python/cpython/pull/125691#discussion_r1807910964

https://github.com/user-attachments/assets/5b99fd79-c25c-4ffb-97f3-5a09e8574339

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

rruuaanng commented 1 month ago

Can you explain it to me? I don't seem to understand it.

terryjreedy commented 1 month ago

(Moved here from PR #125691 and revised): The normal way to stop within the demo framework is the red STOP button, and this works (but see below). The spacebar bit is for when one runs sorting_animate directly, as with f:\dev\3x>python -m turtledemo.sorting_animate. Spacebar does work then. Perhaps other demos should stop with spacebar when run directly. Maybe some do?

The 'but' above is that both STOP and spacebar raise Terminator when pressed while the animation is going, as opposed to when waiting for input (i, etc.). Both should be fixed.

When an example is run directly, if __name__ == "__main__": can run code limited to that mode. 'Space' as a replacement for STOP should only work when there is no STOP.

@rruuaanng Run turtledemo either from IDLE or console (py -m turtledemo on Windows).

Wulian233 commented 1 month ago

Is it acceptable to remove the space- exit? (which is used to run individual demo directly, but other demos do not have this function)

Or is there any other solution?

terryjreedy commented 1 month ago

I gave the 'solution', switching on __name__, above. However, since space-exit works the same as closing the window (and this include raising Terminator), and is unique, I think we can remove it. Problems with STOP (EDIT: and window closing) are not limited to this example, and cataloging and fixing them would be another issue.

Xiaokang2022 commented 2 weeks ago

@terryjreedy

I've created a PR to fix this. PTAL.

And about your comment:

Problems with STOP (EDIT: and window closing) are not limited to this example, and cataloging and fixing them would be another issue.

Do you mean that we need to create a separate issue that closing the window when a single case runtime might throw an error? (when running an animation)

I'm not quite sure what you mean in your comment, and if that's what I said, then I'd be happy to create a new one issue and try my best to solve it.