简体   繁体   English

使用接受程序参数的py2exe创建窗口化exe

[英]Create windowed exe using py2exe that accepts program arguments

py2exe provides a list of options that can be used to customize how the resulting exe is set up. py2exe提供了一个选项列表 ,可用于自定义生成的exe的设置方式。 In particular, it provides the options windows and console , which specify whether the exe should use a console or graphical interface. 特别是,它提供了选项windowsconsole ,它们指定exe应该使用控制台还是图形界面。

I would like my program to hide the console when executed but also accept program arguments. 我希望我的程序在执行时隐藏控制台,但也接受程序参数。

When I create an exe with the windows option and run the program with valid arguments, the program does not seem to execute. 当我使用windows选项创建一个exe并使用有效参数运行该程序时,该程序似乎无法执行。

Here's an example. 这是一个例子。 Let's say my python file is HelloWorld.py : 假设我的python文件是HelloWorld.py

import sys
import getopt

if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'h', ['hello'])
    except getopt.GetoptError:
        sys.exit(2)

    for opt, arg in opts:
        if opt in ('-h', '--hello'):
            print 'Hello world!'

Using the setup setup(console=['HelloWorld.py']) and running the resulting exe with HelloWorld.exe -h outputs Hello world! 使用安装程序setup(console=['HelloWorld.py'])并使用HelloWorld.exe -h运行生成的exe会输出Hello world! .

Using the setup setup(windows=['HelloWorld.py']) and running the resulting exe with HelloWorld.exe -h outputs nothing. 使用安装程序setup(windows=['HelloWorld.py'])并使用HelloWorld.exe -h运行生成的exe不会输出任何内容。

You can find an explanation on how py2exe manages stdout and sterr when it packages applications as console application or windows applications at the following link: 您可以在以下链接中找到有关py2exe在将应用程序打包为控制台应用程序或Windows应用程序时如何管理stdout和sterr的说明:

http://www.py2exe.org/index.cgi/StderrLog http://www.py2exe.org/index.cgi/StderrLog

windows option is for GUI application (wxpython etc) which can catch the stdout and show it in the GUI but without implementing such thing you will need cmd window to print stdout by "print" statement. windows选项适用于GUI应用程序(wxpython等),它可以捕获stdout并在GUI中显示它,但如果不执行此操作,则需要cmd窗口通过“ print”语句打印stdout。 For example, in wxpython you can start the app with redirect=True option to capture the stdout. 例如,在wxpython中,您可以使用redirect = True选项启动应用程序以捕获标准输出。

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

import wx

if __name__ == '__main__':
    app = wx.App(True)

redirect - Should sys.stdout and sys.stderr be redirected? 重定向-是否应重定向sys.stdout和sys.stderr? Defaults to True on Windows and Mac, False otherwise. 在Windows和Mac上默认为True,否则为False。 If filename is None then output will be redirected to a window that pops up as needed. 如果filename为None,则输出将被重定向到根据需要弹出的窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM