2

I would like to build simple GUI apps based in Python that can be distributed to my co-workers. In this process, I learnt about PySimpleGUI27 for Python 2.7 version.

The following is a simple code that produces a window with menu options. I then used PyInstaller to create a build and test it out. However, when I run the .exe build from "dist" folder it just flashes and disappears. But when I run the GUI script from within a Python IDE, I am able to actually see the GUI function.

import PySimpleGUI27 as sg

sg.ChangeLookAndFeel('LightGreen')
sg.SetOptions(element_padding=(0, 0))

# ------ Menu Definition ------ #
menu_def = [['File', ['Open', 'Save', 'Exit']],
            ['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
            ['Help', 'About...'], ]

# ------ GUI Definition ------ #
layout = [
    [sg.Menu(menu_def, )],
    [sg.Output(size=(60, 20))]
]

window = sg.Window("Windows-like program", default_element_size=(12, 1), auto_size_text=False, auto_size_buttons=False,
                   default_button_element_size=(12, 1)).Layout(layout)

# ------ Loop & Process button menu choices ------ #
while True:
    event, values = window.Read()
    if event == None or event == 'Exit':
        break
    print('Button = ', event)
    # ------ Process menu choices ------ #
    if event == 'About...':
        sg.Popup('About this program', 'Version: 1.0', 'PyDist: Anaconda27')
    elif event == 'Open':
        filename = sg.PopupGetFile('file to open', no_window=True)
        print(filename)

GUI generated when I run the script 1

When I try to run the exe file, it flashes and disappears. Please check out the awesome GIF here. Any advise is appreciated.

Running the EXE from the folder using Powershell.

PS C:\Users\user\AppData\Local\Continuum\anaconda3\envs\XCAL\Scripts\dist\PySimpleGUI_00> .\PySimpleGUI_00.exe
Traceback (most recent call last):
  File "PySimpleGUI_00.py", line 1, in <module>
  File "c:\users\user\appdata\local\temp\pip-install-qrr1qq\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
  File "site-packages\PySimpleGUI27\__init__.py", line 2, in <module>
  File "c:\users\user\appdata\local\temp\pip-install-qrr1qq\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
  File "site-packages\PySimpleGUI27\PySimpleGUI27.py", line 13, in <module>
  File "site-packages\future\standard_library\__init__.py", line 459, in install_aliases
ImportError: No module named UserList
[15328] Failed to execute script PySimpleGUI_00
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Rene Duchamp
  • 2,429
  • 2
  • 21
  • 29
  • Run it from a console, so you will see the error – BartoszKP Nov 01 '18 at 15:30
  • 1
    It's throwing an error in a console window and then closing, try activating the exe file from a command line. Open a cmd window in the folder where your exe is and run the file from that by typing "yourexename.exe". It won't launch the program but you will get an error message, please do that and post the error here so we can see what is going on – vencaslac Nov 01 '18 at 15:31
  • @BartoszKP I have added the error details from running the exe in console. ImportError: No module named UserList [15328] Failed to execute script PySimpleGUI_00 – Rene Duchamp Nov 01 '18 at 16:23
  • Possible duplicate of [PyInstaller - no module named](https://stackoverflow.com/questions/25733467/pyinstaller-no-module-named) – BartoszKP Nov 01 '18 at 22:28
  • I was able to build the exe using the command line "pyinstaller -wF test.py". I copied and pasted your code. However, it was using Python 3. I did the import Pysimplegui27 however so all of that code was the same, only the python interpreter itself differed. Any particular reason you're using the 2.7 versions? Another question is have you gotten other EXEs to work using pyinstaller? – Mike from PSG Nov 01 '18 at 23:00
  • @MikeyB No reason to use 2.7. I was just working with 2.7 and just tried it with that. Secondly, this is my first try with pyinstaller and I don't have any previous experience with any Python to executable packages. – Rene Duchamp Nov 02 '18 at 00:32
  • Then for sure use Python 3. If there's no reason to use 2.7, then there's every reason to use 3, especially since I just got your program running as an exe using Python 3. It works great with Python 3. There is an EXE-maker program written in PySimpleGUI that uses PyInstaller. Switch and I think you're problems will go away. If you want to try the EXE Makers it's here - https://github.com/MikeTheWatchGuy/PySimpleGUI/tree/master/exemaker – Mike from PSG Nov 02 '18 at 01:09
  • @MikeyB Okay, let me try to get this done in Py3. – Rene Duchamp Nov 02 '18 at 12:47

1 Answers1

1

I recommend using the Python 3 version of the PySimpleGUI library in addition to using Python 3 to run pyinstaller.

Run pyinstaller with these options

pyinstaller -wF demo.py

Your example should be this to use the plain PySimpleGUI import:

import PySimpleGUI as sg

sg.ChangeLookAndFeel('LightGreen')
sg.SetOptions(element_padding=(0, 0))

# ------ Menu Definition ------ #
menu_def = [['File', ['Open', 'Save', 'Exit']],
            ['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
            ['Help', 'About...'], ]

# ------ GUI Definition ------ #
layout = [
    [sg.Menu(menu_def, )],
    [sg.Output(size=(60, 20))]
]

window = sg.Window("Windows-like program", default_element_size=(12, 1), auto_size_text=False, auto_size_buttons=False,
                   default_button_element_size=(12, 1)).Layout(layout)

# ------ Loop & Process button menu choices ------ #
while True:
    event, values = window.Read()
    if event == None or event == 'Exit':
        break
    print('Button = ', event)
    # ------ Process menu choices ------ #
    if event == 'About...':
        sg.Popup('About this program', 'Version: 1.0', 'PyDist: Anaconda27')
    elif event == 'Open':
        filename = sg.PopupGetFile('file to open', no_window=True)
        print(filename)
Mike from PSG
  • 5,312
  • 21
  • 39