Windows GUI - which way?

Jeff Shannon jeff at ccvcorp.com
Mon Mar 4 14:50:27 EST 2002


Spencer Ernest Doidge wrote:

> I want to maintain and use a library of Python scripts. I want to invoke
> any script from this library by using a GUI app. The script itself would
> have no GUI features. It would be sending and receiving serial port
> messages. (This is like the script I am already running.)
>    ....
> If I write the GUI app all in Python, is it a simple matter to, say, load
> and run the script in its own thread, and have it be responsive to a
> signal from the main thread telling it to abort?

The answers to your question depend on whether you intend to design the library
scripts specifically for this purpose, or if you're intending to use
"off-the-shelf" scripts from elsewhere.  If you're able to modify the scripts to
work with your GUI framework, then all of these things are (fairly) easily
possible.  If you want to be able to run *any* script, then things become more
difficult.

In order to abort the script thread, the script needs to check for a condition and
terminate itself -- you can't kill a thread from outside.  If you don't control
the script, then you have no way to ensure that it will do this, of course.  You
*can* do this by running the script in a separate process, though -- it's a more
expensive solution, but you can kill the process if you need to.

Output is no problem in either case -- for threads, simply write your output to a
Queue, and have your GUI read the queue and update a window.  For arbitrary
scripts, you can use popen(), or more likely popen2() or popen3(), which gives you
out-of-process execution and a handle to that script's stdout.

A progress bar would be no problem for the first case, either -- like Peter said,
just have the script post notices to the GUI when you need to update the progress
bar.  I can't think of any practical way to do this for arbitrary scripts, though,
because you have no way of knowing how long they will run, or what stage they are
at.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list