Muhammad Ali
Muhammad Ali

Reputation: 732

GUI program breaking with pyinstaller & cx_Freeze

My GUI program built with tkinter has the following main part.

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

The DataExtractor calls another function on button click. The said function has multi-processing tasks inside.

The GUI was working perfectly when run from command line.

When compiling to exe using pyinstaller or cx_Freeze, the program keeps on spawning windows 1+number of processes and doesn't work as expected. Following in my pyinstaller spec file:

# -*- 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')

I had the same problem with the command line version in the beginning which was solved by adding if __name__ == "__main__"

With the exe packing tool, it's not clear to me why it's bypassing the __main__ entry point.

Upvotes: 0

Views: 366

Answers (1)

Muhammad Ali
Muhammad Ali

Reputation: 732

Since python multiprocessing launches a new interpreter for every spawned process, the extra windows are created for each time a new process entered if __name__ == "__main__"

For GUI programs this can be solved by adding the following command at the top of the program:

multiprocessing.freeze_support()

The solution reason is described by @codewarrior in his answer: Why python executable opens new window instance when function by multiprocessing module is called on windows

Upvotes: 1

Related Questions