在 Windows 下摆脱 wxPython 中的 Python 控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1310972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:01:24  来源:igfitidea点击:

Getting rid of Python console in wxPython under Windows

pythonwindowsconsolewxpythonwindows-console

提问by Alex

Possible Duplicate:
How can I hide the console window in a PyQt app running on Windows?

可能的重复:
如何在 Windows 上运行的 PyQt 应用程序中隐藏控制台窗口?

How to get rid of the console that shows up as standard output when running wxPython programs in Windows?

在 Windows 中运行 wxPython 程序时,如何摆脱显示为标准输出的控制台?

回答by AlexJReid

Not familiar with wxPython, but if you invoke your script with pythonw.exe rather than python.exe, the console window shouldn't appear. I believe saving the script as script.pyw also works.

不熟悉 wxPython,但如果您使用 pythonw.exe 而不是 python.exe 调用您的脚本,则不应出现控制台窗口。我相信将脚本保存为 script.pyw 也可以。

回答by Francesco

Others have already suggested of renaming from py to pyw. If you instead refer to Output redirection pass redirect=True when creating the wx.App class.

其他人已经建议将 py 重命名为 pyw。如果您在创建 wx.App 类时改为参考输出重定向传递 redirect=True 。

See for instance http://www.wxpython.org/docs/api/wx.App-class.html

参见例如http://www.wxpython.org/docs/api/wx.App-class.html

The signature of __init__is

的签名__init__

__init__(self, redirect=False, filename=None, useBestVisual=False, clearSigInt=True)

__init__(self, redirect=False, filename=None, useBestVisual=False, clearSigInt=True)

If you set redirect=Trueand filename different from None, sys.stdoutand sys.stderrwill be redirect to filename. Note that on Windows and Mac redirect default value is True. If redirect==True and filename is None, the output will be printed in a popup window different from your other frames. This can be very useful so that while debugging you can follow what is happening, while not cluttering the user interface with the internals of your app in release mode.

如果设置redirect=True和无文件名不同,sys.stdout并且sys.stderr会重定向到文件名。请注意,在 Windows 和 Mac 上重定向默认值为 True。如果redirect==True and filename is None,则输出将打印在与其他框架不同的弹出窗口中。这非常有用,因此在调试时您可以跟踪正在发生的事情,同时不会在发布模式下使用户界面与应用程序的内部结构混乱。

回答by DrBloodmoney

The easiest way to do this:

最简单的方法:

if __name__ == "__main__":
    app = wx.App(0) #<--- or False
    frame = MyFrame('My Frame')
    frame.Show(True)
    app.MainLoop()

This prints to stdout instead of the wxPython window.

这将打印到 stdout 而不是 wxPython 窗口。

回答by Dave Webb

I don't know wxPython but the solution might be as simple as using pythonw.exeto run the program instead of python.exe.

我不知道 wxPython,但解决方案可能就像使用pythonw.exe运行程序而不是python.exe.