使用TKINTER和CX_FREEZE隐藏控制台窗口

发布于 2024-10-28 23:28:28 字数 270 浏览 2 评论 0原文

我正在使用 cx_freeze 冻结 tkinter 应用程序。当我运行 exe 时,我得到一个非常无用的控制台窗口以及我的 tkinter GUI。

我想删除/隐藏这个无用的黑色窗口。

我见过建议以下内容的线程:

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

上面的代码与我想要的相反。它隐藏了我的 GUI,而无用的黑色窗口仍然存在。我希望情况正好相反。

I am using cx_freeze to freeze a tkinter app. When I run the exe I get a wonderfully USELESS console window along with my tkinter GUI.

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

I've seen threads that suggest 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

海的爱人是光 2024-11-04 23:28:28

我记得在某处读过,在 Windows 上,如果您将文件扩展名指定为 .pyw,它将使用 pythonw.exe 启动(没有控制台窗口)。这对你有用吗?

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

困倦 2024-11-04 23:28:28

这个问题非常相似,但是针对wxPython和cx_Freeze。幸运的是,事实证明控制台的外观可以从构建脚本配置,而不是源代码。借用前两个答案,技巧是在 cx_Freeze 构建脚本中设置 base 变量:

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.>

这是相关的 文档(尽管它没有明确提及 base 控制控制台选项)。

另外,仅仅因为它很有趣,对不同问题的答案解决了使用或创建 GUI 应用程序的问题没有控制台模式选项,我认为这非常酷。

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

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.>

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

Also, just because it's interesting, an answer to a different question solves the issue of creating a GUI app with or without a console mode option, which I thought was very cool.

半边脸i 2024-11-04 23:28:28

就像加里说的那样,然后:

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

这将起作用 cx_Freeze

Do exactly just like 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

甜尕妞 2024-11-04 23:28:28

如果使用 pyinstaller 使用 pyinstaller-gui.py
在 Windows 命令行中输入

python pyinstaller-gui.py

这首先会说“请仅使用‘pyinstaller.py’。Gui 不被维护。”更改代码 l'il 位,您将能够运行它。

它将弹出一个窗口来选择您的脚本和一些复选框。检查“没有控制台(仅限Windows)”

就是这样。你完成了!

另一种选择:在构建时使用 --noconsole 选项。 IE:

python pyinstaller.py --noconsole yourscript.py

If using pyinstaller use pyinstaller-gui.py
In Windows command line type

python pyinstaller-gui.py

This will first say "Please use just 'pyinstaller.py'. Gui is not maintained." Change the code l'il bit and you will be able to run this.

It will show pop up a window to select your script and some checkboxex. Check on 'no console(windows only)

That's it. You are done!

Another option: use --noconsole option while building. i.e:

python pyinstaller.py --noconsole yourscript.py

×眷恋的温暖 2024-11-04 23:28:28

我今天遇到了同样的问题,

我用来编译 python 程序的是 py2exe,修复方法非常简单,修改安装文件,如下所示。我的界面是用 Tkinter 编写的,

修改“setup.py”py2exe 脚本:

旧 Python 代码:

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

新 Python 代码:

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

在我执行此操作并重新运行我的安装脚本后,应用程序已加载并且没有显示控制台窗口。唯一的问题是,如果您的应用程序向控制台窗口发送打印命令,您将看不到主题。我希望这有帮助。

I had the same problem today

What i was using to compile my python programs was py2exe and the fix was very simple modify the setup file as shown below. My interface is written with Tkinter

modify the "setup.py" py2exe script from:

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 reran my setup script the application loaded and did not show the console window. The only thing with this is if you have your application sending print commands to the console window you will not see theme. I hope this helps.

墨小沫ゞ 2024-11-04 23:28:28

对我来说,使用选项 --base-name Win32GUI 有效。这是一个例子:

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

For me using the option --base-name Win32GUI works. Here is an example:

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

梦里°也失望 2024-11-04 23:28:28

我假设您所说的“黑色窗口”指的是终端窗口。为了禁止弹出此窗口,请将文件另存为 .pyw 扩展名而不是 .py

I'm assuming by "black window" you are referring to the terminal window. In order to disable this from popping up, save your file as a .pyw extension instead of .py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文