【问题标题】:Hiding command prompt in wxpython app在 wxpython 应用程序中隐藏命令提示符
【发布时间】:2024-01-19 03:53:01
【问题描述】:

我使用 wxpython 编写了一个 gui 应用程序,并使用 py2exe 将其转换为可执行文件。但是,每当我运行可执行文件时,命令提示符都会在后台出现并在我退出程序时关闭。有什么办法不让它出现?

【问题讨论】:

    标签: python python-2.7 cmd wxpython py2exe


    【解决方案1】:

    您在 py2exe 设置中将应用程序类型设置为 console

    setup(console=...
    

    虽然你需要做到windows:

    setup(windows=...
    

    我在一个名为example.pyw的文件中有一个简单的wxpython应用程序:

    # -*-coding: utf-8 -*-
    #!python
    
    # @file: example.pyw
    
    import wx
    
    class AppFrame(wx.Frame):
      def __init__(self, title, *args, **kwargs):
        super(AppFrame, self).__init__(None, title=title, *args, **kwargs)
        self.panel = wx.Panel(self)
    
    
    class AppMain(wx.App):
      def OnInit(self):
        self.frame = AppFrame("Example")
        self.frame.Show()
        return True
    
    
    AppMain(True).MainLoop()
    

    这是一个名为 setup.py 的文件中的简单设置:

    # -*-coding: utf-8 -*-
    #!python
    
    # @file: setup.py
    
    from distutils.core import setup
    import py2exe
    
    # http://msdn.microsoft.com/en-us/library/ms648009%28v=vs.85%29.aspx
    RT_MANIFEST = 24
    
    # http://en.wikipedia.org/wiki/Side-by-side_assembly
    manifest = """
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <dependency>
      <dependentAssembly>
        <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
      </dependentAssembly>
    </dependency>
    <dependency>
      <dependentAssembly>
        <assemblyIdentity
          type="win32"
          name="Microsoft.VC90.CRT"
          version="9.0.21022.8"
          processorArchitecture="*"
          publicKeyToken="1fc8b3b9a1e18e3b"
          language="*"
        />
      </dependentAssembly>
    </dependency>
    </assembly>
    """
    
    setup(
      windows = [{
        "script" : "example.pyw",
        # "icon_resources" : [(1, "icon.ico")] # have an icon?
        "other_resources" : [(RT_MANIFEST, 1, manifest)]
      }]
    )
    

    将它们放在同一目录中,并将cd 放到该目录并运行python setup.py py2exe

    > python setup.py py2exe
    running py2exe
    creating C:\Users\XXX\build
    creating C:\Users\XXX\build\bdist.win32
    ...
       KERNEL32.dll - C:\Windows\system32\KERNEL32.dll
       MSVCP90.dll - C:\Program Files\CMake 2.8\bin\MSVCP90.dll
       RPCRT4.dll - C:\Windows\system32\RPCRT4.dll
    > 
    

    如果一切顺利,您应该有一个名为dist 的文件夹,其中包含example.exe。运行它,它应该会显示没有控制台的窗口。

    【讨论】:

    • 我不确定,但我认为较新版本的 wx 不再需要 SxS 清单。我在想那只是在 Python 2.6 和 wx 2.8
    • 甜!!非常感激。不过,我不得不注释掉“other_resources”这一行。在 cmd 中向我显示“SyntaxError:无效语法”。