I have a module which is supposed to runboth in console mode as well as in
wx. If a "wx environent" is detected, it will interact via gui widgets,
else via text mode interface.
Question: how can I detect whether my module is running in such a wx
environment (= a runing wxApp)?
I have a module which is supposed to runboth in console mode as well as in
wx. If a "wx environent" is detected, it will interact via gui widgets,
else via text mode interface.
Question: how can I detect whether my module is running in such a wx
environment (= a runing wxApp)?
I suppose you could look through sys.modules to see if wxPython has been
imported.
One would expect that this tests the availability of the wxPython
module, not whether the current module was loaded by something that
has already got wxPython loaded and in use.
However, in my experience, it doesn't even work for that--if wxPython
isn't installed, an ImportError will be raised, and the above code
will work. If wxPython is installed and able to display, the above
code will work. If wxPython is installed but cannot display (ie: it's
trying to use X11 and cannot talk to the X server specified by
$DISPLAY), the above code will cause Python to exit.
···
On Wed, Feb 27, 2002 at 11:27:41AM -0800, Cliff Wells wrote:
On Tue, 26 Feb 2002 15:31:49 +1100 > Horst Herb wrote:
> I have a module which is supposed to runboth in console mode as
> well as in > wx. If a "wx environent" is detected, it will
> interact > via gui widgets, > else via text mode interface.
>
> Question: how can I detect whether my module is running in such a wx
> environment (= a runing wxApp)?
how about
from wxPython import wx
try:
wx
# or if you use from wxPython.wx import *
# wxApp
except:
print "text mode"
else:
print "graphical mode"
An extreme and horribly inefficient approach, if checking sys.modules
for the wxPython modules is somehow inadequate, would be to dump a
stack traceback into a string, and match against the app's loop
function. If you're inside the main loop of a wxApp, I'm pretty sure
the stack traceback is going to show that.
I think the EASIEST way to handle it, though, is probably to set a
global variable is_gui in __main__ if you fire up the GUI, and set
that variable to a different value if you fire up the curses
interface. Then you can import __main__ and __main__.is_gui will
reflect which state you're in. At some point, you're bound to have a
hint, and sharing that information with yourself is easier than trying
to derive it on the fly.
Oops (what was I thinking?). Then probably Robin's suggestion is best:
import sys
if sys.modules.has_key("wxPython.__version__"): # or something
print "graphical mode"
else:
print "text mode"
···
On Wed, 27 Feb 2002 15:09:37 -0500 Joshua Judson Rosen wrote:
One would expect that this tests the availability of the wxPython
module, not whether the current module was loaded by something that
has already got wxPython loaded and in use.
I am already doing a "sys.modules['wxPython']" and catch the key error.
HOwever, as many of my modules load wxPython, and the order these modules
are imported is unpredictable (plug-ins), I still need to know wether an
instance of wxApp is already initialized and the main loop running. HOw
can I do this. No such function as GetApp() ?
Horst
···
On Thu, 28 Feb 2002 06:31, Robin Dunn wrote:
> I have a module which is supposed to runboth in console mode as well
> as in wx. If a "wx environent" is detected, it will interact via gui
> widgets, else via text mode interface.
>
> Question: how can I detect whether my module is running in such a wx
> environment (= a runing wxApp)?
I suppose you could look through sys.modules to see if wxPython has been
imported.
I am already doing a "sys.modules['wxPython']" and catch the key error.
HOwever, as many of my modules load wxPython, and the order these modules
are imported is unpredictable (plug-ins), I still need to know wether an
instance of wxApp is already initialized and the main loop running. HOw
can I do this. No such function as GetApp() ?
Hold on, let be access my time machine... Okay, you can use wxGetApp().