Attached: user.py, which imports wxvisual.py.There is some semi-outrageous but apparently totally legal black magic to make this work on all platforms, including in particular on Mac Cocoa.
I tried a number of architectures. A particularly clean one goes back to an early suggestion by Robin, which is to have the wxvisual module spawn a process “python wxhelper user.py”. The wxvisual was basically just a stub to set up the spawn. I got this to work, but with the unacceptable side effect that when run from IDLE the print output went to a terminal, not to the IDLE shell window. Maybe I could get around this somehow, but it wasn’t clear how.
The architecture represented by the attached files looks like this:
user.py imports wxvisual
wxvisual sets up the wxpython GUI environment but before calling MainLoop it also sets up and starts a secondary thread, which does an exec of modified source code from the user file.
The modifications to the user’s source, which are done before creating and starting the secondary thread, consist of finding all import statements and executing them, using the results of the imports, in globals(), to create a special globals environment for the exec. The code is a bit bizarre but necessary because I found experimentally that (1) it is impossible to execute an import statement in a secondary thread and (2) import statements executed in a module must be in the base level of the module, not inside a function (hence the code looks yucky).
Now that I have this architecture worked out for a simple test case, I will try to replace the GUI portions of VPython (vpython.org) with this wxpython framework, so that it is possible to run on Cocoa on the Mac (the attached files run fine on Windows, Mac Cocoa, and Ubuntu). The goal of VPython (and the web-based GlowScript, at glowscript.org) is to make it feasible for non-expert programmers to write simple programs that generate real-time navigable 3D animations. The contents of the attached user.py give a sense of what VPython and GlowScript aim to do in making graphics-oriented programming accessible to ordinary mortals. This generates a rotating line:
import wxvisual as w
rod = w.line()
while 1:
w.rate(100)
rod.angle += .01
This hides all of the GUI setup, which is beyond novice programmers.
Again, many thanks to all of you who helped me learn my way around wxPython (and around some aspects of Python, too). Special thanks to Robin for particularly informative suggestions, and for wxPython itself.
user.py (121 Bytes)
wxvisual.py (7.97 KB)