Application in console and gui mode
I have a python console application. Like most python console applications, it uses the OptionParser module to accept arguments. I have now developed a GUI for my application using wxPython and I would like to integrate the two. I want my app to run from both the console and the OS UI. When called from the console, it launches as a console application, and when double-clicked in the OS UI, it launches as a graphical application. How could I do something like this? Can anyone show me a snippet of how the block should look like __main__
?
Thanks a ton.
a source to share
can you pass args to the application and 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 wrong, I thought you wanted to start with a different Wx app. and not from "OS UI", I don't know of a big, cross-platform way to do this. The problem is that on windows, the .py
files are usually associated with python.exe files .pyw
, similar but don't have a console window.
Therefore, you will really need to change the OS to support this behavior. For example, you can create a shortcut (in windows / gnome / kde) that launches the program with --gui_mode or uses a mechanism like @Austin offered in the * nix operating system. Some of this stuff can be automated with disttools if you install the app
a source to share
Try:
import os
print os.environ
and you have os.environ ['TERM'] output to the tkinter window when you execute your script by double clicking it.
For me it is "xterm-color".
What operating system are you using? How do you ensure that double clicking on the .py file will execute it?
a source to share