【问题标题】:GUI program breaking with pyinstaller & cx_Freeze使用 pyinstaller 和 cx_Freeze 破坏 GUI 程序
【发布时间】:2018-12-18 14:26:07
【问题描述】:

我用 tkinter 构建的 GUI 程序有以下主要部分。

if __name__ == '__main__':
    root = Tk()
    my_gui = DataExtractorUI(root)
    root.mainloop()

DataExtractor 在按钮单击时调用另一个函数。该函数内部具有多处理任务。

从命令行运行时,GUI 运行良好。

当使用 pyinstaller 或 cx_Freeze 编译为 exe 时,程序会不断生成 windows 1+进程数并且不能按预期工作。按照我的 pyinstaller 规范文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['MainGUI.py'],
             pathex=['.'],
             binaries=[],
             datas=[('logo/m3_logo.png', 'logo')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='MainGUI',
          debug=False,
          strip=False,
          upx=True,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='MainGUI')

我一开始的命令行版本也有同样的问题,通过添加if __name__ == "__main__"解决了

使用 exe 打包工​​具,我不清楚它为什么绕过 __main__ 入口点。

【问题讨论】:

    标签: python tkinter pyinstaller python-multiprocessing cx-freeze


    【解决方案1】:

    由于 python 多处理为每个生成的进程启动一个新的解释器,因此每次新进程进入时都会创建额外的窗口if __name__ == "__main__"

    对于 GUI 程序,可以通过在程序顶部添加以下命令来解决此问题:

    multiprocessing.freeze_support()
    

    @codewarrior 在他的回答中描述了解决方案的原因: Why python executable opens new window instance when function by multiprocessing module is called on windows

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-11
      • 2013-07-06
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多