Hide console window using Tkinter and cx_Freeze

I am using cx_freeze to freeze a tkinter application. When I run exe, I get a wonderfully IMPOSSIBLE console window along with my tkinter GUI.

I would like to remove / hide this unnecessary black window.

I have seen topics that offer the following:

root = tkinter.Tk() root.withdraw() 

The above code does the opposite of what I want. It hides my GUI while the useless black window remains. I would like it to be the other way around.

+7
source share
7 answers

This question is very similar, but for wxPython and cx_Freeze. Fortunately, it turns out that the appearance of the console can be customized from the build script, rather than the source code. Borrowing from the top two answers, the trick sets the base variable in your cx_Freeze script assembly:

 import sys from cx_Freeze import setup, Executable base = None if (sys.platform == "win32"): base = "Win32GUI" # Tells the build script to hide the console. # <The rest of your build script goes here.> 

The relevant documentation is below (although it does not explicitly mention that base controls the console option).

Also, just because it’s interesting, the answer to another question solves the problem of creating a GUI application with or without console mode option, which I thought was very cool. Sub>

+7
source

I remember reading somewhere that on Windows, if you specify the extension of your file as .pyw , it will start with pythonw.exe (without a console window). Does this work for you?

+13
source

Do exactly what Gary said, then:

 setup(name="ur package name", version="ur package version", description="as above", executables=[Executable("ur_script.py", base=base)] 

This will work cx_Freeze

+4
source

I had the same problem today

What I used to compile my python programs was py2exe, and the fix was very simple by modifying the installation file as shown below. My interface is written using Tkinter

change the "setup.py" py2exe script to:

Old Python code:

 from distutils.core import setup import py2exe setup(console=['app.py']) 

New Python code:

 from distutils.core import setup import py2exe setup(windows=['app.py']) 

After I did this and restarted my script setup, the application loaded and did not display the console window. The only thing connected with this, if you have an application that sends print commands to the console window, you will not see the topic. Hope this helps.

+2
source

I assume that in the "black window" you are referring to the terminal window. To prevent this from popping up, save the file as a .pyw extension instead of .py

+1
source

When using pyinstaller, use pyinstaller-gui.py at the windows command prompt

python pyinstaller-gui.py

First, say: "Please use only" pyinstaller.py ". Gui is not supported." Change the l'il bit code and you can run it.

A popup will appear to select your script and some checkboxex. Check "no console (windows only)

What is it. All is ready!

Another option: when creating, use the --noconsole option. i.e:

python pyinstaller.py --noconsole yourscript.py

+1
source

The option --base-name Win32GUI used for me. Here is an example:

cxfreeze your_python_file.py --base-name Win32GUI --target-dir your_target_dir

0
source

All Articles