views:

86

answers:

2

Hi,

I have a python console app. Like most python console apps it uses the OptionParser module to take arguments. I've now developed a GUI for my app using wxPython and i'd like to integrate the two. I'd like my app to be run both from the console and from the OS's UI. When it is invoked from the console it runs as a console app and when it is double clicked in the OS's UI, it runs as a GUI app. How could I do something like this? Could someone show me a a snippet of what the __main__ block should be like?

Thanks a ton.

+2  A: 

can you pass args to the app then use the arg parser?

if __name__ == "__main__":
  from  optparse import OptionParser

  parser = OptionParser() 
  parser.add_option("-g","--gui_mode",
                    dest="guimode",
                    help="start program in gui mode",
                   action="store_true")

  (options,args) = parser.parse_args()

  if (options.guimode):
      print "start wx app"
  else:
      print "start cmd line app"

Edit: Sorry, misread, i thought you wanted to start from another Wx App. rather than from the "OS UI" I don't know of a great, cross platform way to do this. The issue is that in windows .py files are usually associated with the python.exe .pyw files are similar, but don't have a console window.
So you would actually have to modify the OS to support this behaviour. For example you could create a shortcut (in windows/gnome/kde) that launches the program with --gui_mode or use a mechanism like @Austin suggested in an *nix OS. Some of this stuff can be automated by disttools if you are installing an application

iondiode
A: 

Try:

import os
print os.environ

and have it output os.environ['TERM'] to a tkinter window when you execute the script by double-clicking it.

For me, it's 'xterm-color'.

What operating system are you using? How do you ensure that double-clicking a .py file will result in its execution?

Austin