问题描述
我有一个带有 wxpython GUI 和一些命令行参数的 python 程序.我用 py2exe 生成了一个单一的 Windows 可执行文件.我不想在后台有一个命令行窗口,所以 py2exe 使它成为没有这个窗口的 pythonw 可执行文件.这相当于使用 *.pyw 扩展名.
I have a python program with a wxpython GUI and some command line parameters. I generate a single windows executable with py2exe. I don't want to have a command line window in the background, so py2exe is making this a pythonw executable without this window. This is equivalent to use the *.pyw extension.
问题是,如果您想查看可用的命令行参数,您自然会在 shell 上执行main.exe -h".即使 argparse 提供了这些信息,它也不会因为 *.pyw 扩展名而到达标准输出.
The problem is, if you want to see the available command line arguments, you naturally do "main.exe -h" on a shell. Even though argparse is providing this information, it doesn't reach stdout because of the *.pyw extension.
那么如何为使用 pythonw 的 GUI 应用程序重新启用标准输出?
So how could I re-enable stdout for a GUI application using pythonw?
最小工作示例:
# test.py
print "hello"
执行:
#> python test.py
hello
#> pythonw test.py
#>
预先感谢您的任何建议!
Thanks in advance for any suggestion!
推荐答案
我终于用某种讨厌的技巧解决了我的问题.我从 argparse 那里得到了这样的帮助信息:
I finally solved my problem with some kind of a nasty trick. I get the help information from argparse like that:
class Parser(object):
def __init__(self):
[...init argparser...]
self.help = ""
self.__argparser.print_help(self)
def write(self, message):
self.help += message
然后我只在关于对话框中显示帮助信息.
Then I just show the help information in the about dialog.
我仍然希望重新启用 sys.stdout
,但这暂时有效.感谢所有建议!
I would still prefer to re-enable sys.stdout
, but this works for now.Thanks to all suggestions!
这篇关于如何从带有 .pyw 扩展名的 Python 脚本打印到标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!