如何使用cx_Freeze冻结双模式(GUI和控制台)应用程序? [英] How can I freeze a dual-mode (GUI and console) application using cx_Freeze?
问题描述
我已经开发了可以在GUI模式和控制台模式下运行的Python应用程序。如果指定了任何参数,它将在控制台模式下运行,否则将在GUI模式下运行。
I've developed a Python application that runs both in the GUI mode and the console mode. If any arguments are specified, it runs in a console mode else it runs in the GUI mode.
我设法使用cx_Freeze将其冻结。我在隐藏将随wxPython弹出的黑色控制台窗口时遇到了一些问题,因此我修改了 setup.py
脚本,如下所示:
I've managed to freeze this using cx_Freeze. I had some problems hiding the black console window that would pop up with wxPython and so I modified my setup.py
script like this:
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)])
这很好用,但是现在当我尝试打开控制台并从那里运行可执行文件时,它什么也不输出。我没有收到任何错误或消息,因此cx_Feeze似乎正在将stdout重定向到其他地方。
This works fine but now when I try to open up my console and run the executable from there, it doesn't output anything. I don't get any errors or messages so it seems that cx_Feeze is redirecting the stdout somewhere else.
是否可以使其同时在两种模式下工作?似乎在任何地方都没有类似的记录。 :(
Is is possible to get it to work with both mode? Nothing similar to this seems to be documented anywhere. :(
谢谢。
Mridang
推荐答案
我在此页面上发现了这一点:
I found this bit on this page:
无控制台版本的提示:如果
您尝试打印任何内容,则
会显示一个讨厌的错误窗口,因为
stdout和stderr不存在(而
cx_freeze Win32gui.exe存根将
显示一个错误窗口)。当您希望程序成为
能够以GUI模式和
命令行模式运行。要安全地禁用
控制台输出,请在程序的
开头执行以下操作:
Tip for the console-less version: If you try to print anything, you will get a nasty error window, because stdout and stderr do not exist (and the cx_freeze Win32gui.exe stub will display an error Window). This is a pain when you want your program to be able to run in GUI mode and command-line mode. To safely disable console output, do as follows at the beginning of your program:
try:
sys.stdout.write("\n")
sys.stdout.flush()
except IOError:
class dummyStream:
''' dummyStream behaves like a stream but does nothing. '''
def __init__(self): pass
def write(self,data): pass
def read(self,data): pass
def flush(self): pass
def close(self): pass
# and now redirect all default streams to this dummyStream:
sys.stdout = dummyStream()
sys.stderr = dummyStream()
sys.stdin = dummyStream()
sys.__stdout__ = dummyStream()
sys.__stderr__ = dummyStream()
sys.__stdin__ = dummyStream()
这样,如果程序以
-less控制台模式启动,如果代码包含打印语句,它甚至可以在
下工作。
如果以命令行模式运行,则
将照常打印出来。 (这是
,基本上就是我在webGobbler中所做的,
也是。)
This way, if the program starts in console-less mode, it will work even if the code contains print statements. And if run in command-line mode, it will print out as usual. (This is basically what I did in webGobbler, too.)
这篇关于如何使用cx_Freeze冻结双模式(GUI和控制台)应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!