问题
I have a GUI script run.py which i turned into a .exe file using pyinstaller. When I double click on the .exe file two windows are opened. First the main GUI of my script and second the empty black cmd window on background.
I saw on forums that saving run.py as run.pyw and then converting it into .exe file will solve the problem. But in my case the problem remains same.
Does anyone have any idea how to get rid of this empty cmd window on background when i run my .exe file?
回答1:
try to build your file .exe with pyinstaller with the options --noconsole
https://pyinstaller.readthedocs.io/en/latest/usage.html#windows
回答2:
Start the script with with pythonw
instead of python
to avoid the shell showing up. This actually applies to Python 2.x
and 3.x
.
See pythonw.exe or python.exe?
If you would like to start the script by double clicking the file you can associate the file extension to be opened with pythonw.exe
instead of python.exe
. This will apply to all your *.py
files!
Rightlick run.py
, Open with
, Choose another app
, More apps
, Look for another app on this PC
. Locate your pythonw.exe
- it's next to your python.exe
. Then check the checkbox Always use this app to open .py files
.
To avoid associating this behaviour with all your *.py
files, change the extension of run.py
to e.g .pyw
and then apply the described steps.
If you don't know where your python.exe
is but you can use it from the terminal then open PowerShell and enter (Get-Command pythonw.exe).Source
to find out.
This only applies to your computer - if you want this to work on all machines you copy your file to, please see Fabrizios answer.
来源:https://stackoverflow.com/questions/55869789/python-2-7-how-to-get-rid-of-cmd-window-while-running-a-python-gui-exe-script