问题
I am developing a simple standalone, graphical application in python. My development has been done on linux but I would like to distribute the application cross-platform.
I have a launcher script which checks a bunch of environment variables and then sets various configuration options, and then calls the application with what amounts to python main.py
(specifically os.system('python main.py %s'% (arg1, arg2...))
)
On OS X (without X11), the launcher script crashed with an error like Could not run application, need access to screen
. A very quick google search later, the script was working locally by replacing python main.py
with pythonw main.py
.
My question is, what is the best way to write the launcher script so that it can do the right thing across platforms and not crash? Note that this question is not asking how to determine what platform I am on. The solution "check to see if I am on OS X, and if so invoke pythonw instead" is what I have done for now, but it seems like a somewhat hacky fix because it depends on understanding the details of the windowing system (which could easily break sometime in the future) and I wonder if there is a cleaner way.
This question does not yet have a satisfactory answer.
回答1:
If you save the file as main.pyw
, it should run the script without opening up a new cmd/terminal.
Then you can run it as python main.pyw
回答2:
Firstly, you should always use .pyw
for GUIs.
Secondly, you could convert it to .exe
if you want people without python to be able to use your program. The process is simple. The hardest part is downloading one of these:
- for python 2.x: p2exe
- for python 3.x: cx_Freeze
You can simply google instructions on how to use them if you decide to go down that path.
Also, if you're using messageboxes
in your GUI, it won't work. You will have to create windows/toplevels instead.
来源:https://stackoverflow.com/questions/23659248/python-or-pythonw-in-creating-a-cross-platform-standalone-gui-app