TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: ALL
from: DENNIS LEE BIEBER
date: 2019-12-16 12:46:00
subject: Re: Python & thonny - how

On Mon, 16 Dec 2019 12:48:43 +0100, "R.Wieser" 
declaimed the following:

>Hello all,
>
>I'm currently using Thonny to write and execute my Python scripts in, and
>would like to be able to exit a loop by me pressing a key or button
>somewhere, so I can terminate the script graciously (run some cleaning-up
>code).
>
 Let's back up a bit...

 How would you "terminate the script graciously (run some cleaning-up
code)" if you ran your script from the command line/shell?

 Most IDEs run scripts by spawning a process and connecting stdin/stdout
to that process (so they can pass keystrokes to the process and capture
output for display in some console window of the IDE).


>Does Thonny offer any such thing  (apart from its way-too-crude "stop"
>button I mean) ?
>
 I suspect that "stop" button is the equivalent of sending a  to
the running process -- just like you'd do in a command line/shell.

>If not, is there any other way ?    Directly reading the keyboard directly
>requires root, and I've not seen any kind of non-blocking(!) "OK" message
>boxes.
>

 The most common would be to wrap your script in an exception handler,
trapping KeyboardInterrupt
"""
exception KeyboardInterrupt

    Raised when the user hits the interrupt key (normally Control-C or
Delete). During execution, a check for interrupts is made regularly. The
exception inherits from BaseException so as to not be accidentally caught
by code that catches Exception and thus prevent the interpreter from
exiting.
"""

try:
 all
 your
 code
except KeyboardInterrupt:
 cleanup
 code
 sys.exit()


--
 Wulfraed                 Dennis Lee Bieber         AF6VN
 wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.