如何在使用cxFreeze冻结wxPython应用程序时隐藏控制台窗口?

31 投票
4 回答
18437 浏览
提问于 2025-04-15 22:58

我正在用wxPython开发一个Python应用程序,并使用cxFreeze将其打包成可执行文件。一切看起来都很顺利,但有一个问题:

当我运行cxFreeze创建的可执行文件时,会弹出一个空白的控制台窗口。我不想让这个窗口显示出来。有没有办法可以把它隐藏呢?

在cxFreeze的网站上似乎没有相关的文档,网上搜索也没找到太多有用的信息,除了和Py2Exe相关的一些类似问题。

谢谢。

4 个回答

3

如果你在用Windows系统,可以把你那个“主”脚本的后缀名改成.pyw,这个脚本是用来启动应用的。

22

对于Windows系统:

你需要使用类似下面这样的命令(根据你的文件夹和文件名进行调整)

C:/Python/Scripts/cxfreeze C:/Python/Code/yourprogram.py --base-name=Win32GUI --target-dir C:/Python/Dist

通过添加 --base-name=Win32GUI 这个选项,控制台窗口就不会出现了。

20

这个方法在某种程度上有效,但也有问题。我的程序可以在控制台模式和图形界面模式下运行。当我从控制台运行程序并加上 --console 参数时,它就会以控制台模式运行。但是按照下面的步骤操作后,这个功能就不再有效了,我的程序只能作为图形界面应用运行。

以下的源代码来自于 \Python\Lib\site-packages\cx_Freeze\samples\PyQt4\setup.py 这个示例文件。今天的教训是:一定要阅读 README 文件。

# A simple setup script to create an executable using PyQt4. This also
# demonstrates the method for creating a Windows executable that does not have
# an associated console.
#
# PyQt4app.py is a very simple type of PyQt4 application
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
# subdirectory that contains the files needed to run the application

import sys

from cx_Freeze import setup, Executable

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(
        name = "simple_PyQt4",
        version = "0.1",
        description = "Sample cx_Freeze PyQt4 script",
        executables = [Executable("PyQt4app.py", base = base)])

撰写回答