使用TKINTER和CX_FREEZE隐藏控制台窗口
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我记得在某处读过,在 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?这个问题非常相似,但是针对wxPython和cx_Freeze。幸运的是,事实证明控制台的外观可以从构建脚本配置,而不是源代码。借用前两个答案,技巧是在 cx_Freeze 构建脚本中设置
base
变量:这是相关的 文档(尽管它没有明确提及
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: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.
就像加里说的那样,然后:
这将起作用 cx_Freeze
Do exactly just like gary said, then:
This will work cx_Freeze
如果使用 pyinstaller 使用 pyinstaller-gui.py
在 Windows 命令行中输入
这首先会说“请仅使用‘pyinstaller.py’。Gui 不被维护。”更改代码 l'il 位,您将能够运行它。
它将弹出一个窗口来选择您的脚本和一些复选框。检查“没有控制台(仅限Windows)”
就是这样。你完成了!
另一种选择:在构建时使用 --noconsole 选项。 IE:
If using pyinstaller use pyinstaller-gui.py
In Windows command line type
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 程序的是 py2exe,修复方法非常简单,修改安装文件,如下所示。我的界面是用 Tkinter 编写的,
修改“setup.py”py2exe 脚本:
旧 Python 代码:
新 Python 代码:
在我执行此操作并重新运行我的安装脚本后,应用程序已加载并且没有显示控制台窗口。唯一的问题是,如果您的应用程序向控制台窗口发送打印命令,您将看不到主题。我希望这有帮助。
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:
New Python Code:
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.
对我来说,使用选项
--base-name Win32GUI
有效。这是一个例子:For me using the option
--base-name Win32GUI
works. Here is an example:我假设您所说的“黑色窗口”指的是终端窗口。为了禁止弹出此窗口,请将文件另存为 .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