2

I'm using the codes below in a run.py file from PySimpleGUI cookbook for the GUI of my Python app. The GUI works normally when I started the run.py file from PyCharms. However, when I build this into a one file, windowed app with PyInstaller, I get an app crashed error message everytime I close the app with the Exit button on the GUI. I tried to build the app as one file, console app and there wasn't any issue. Has anyone seen this problem before and how to resolve this issue?

def main():
    sg.theme('Tan Blue')

    layout = [
        [sg.Text('Root Path:', size=(15, 1)), sg.InputText(root_path), sg.FolderBrowse()],
        [sg.Text('Date:', size=(15, 1)), sg.InputText(datetime.today().strftime('%Y-%m-%d %H:%M:%S')),
         sg.CalendarButton('Choose Date', target=(1, 1), key='date')],
        [sg.Output(size=(80, 15), key = '_output_')],
        [sg.Button('Run'), sg.Button('Exit'), sg.Button('Clear')]
    ]

    window = sg.Window('App name', layout)

    while True: # Event Loop
        event, values = window.Read()
        if event in (None, 'Exit'):
            break
        elif event == 'Run':
            runScript(values)
            window.Refresh()
        if event == 'Clear':
            window.FindElement('_output_').Update('')
    window.close()


def runScript(values):
    """ run shell command
    @param cmd: command to execute
    @param timeout: timeout for command execution
    @param window: the PySimpleGUI window that the output is going to (needed to do refresh on)
    @return: (return code from command, command output)
    """

    ### My app codes here


main()

I can't include the codes for my app. I use Numpy and Pandas in my codes. I'm listing the dependencies and versions I'm using below

altgraph==0.17
et-xmlfile==1.0.1
future==0.18.2
jdcal==1.4.1
numpy==1.18.4
openpyxl==3.0.3
pandas==1.0.4
pefile==2019.4.18
PyInstaller==3.6
PySimpleGUI==4.19.0
python-dateutil==2.8.1
pytz==2020.1
pywin32==227
pywin32-ctypes==0.2.0
six==1.15.0
xlrd==1.2.0
  • Why don't you post this question on the developer's '[Github](https://github.com/PySimpleGUI/PySimpleGUI/issues)' issue? – r-beginners Jun 03 '20 at 09:51
  • @r-beginners This doesn't seem to be a PySimpleGUI issue because the program works without problems from IDE or from a console. I have a similar experience with our .app built using py2app: from event loop, we run 10+ various functions and everything worked well until recently. Now we've added a function reading a text with emoji's from a website; in PyCharm, from console, or from the binary inside the .app it still works without problems, but the .app terminates immediately on any emoji character. No idea how to solve it and why it's different. Alex Huong Tran, have you found a solution? – Lenka Čížková Mar 21 '21 at 21:45

1 Answers1

0

Does runScript() open a Window which a user can Exit from? If so, it may be that the original window from main() does not close. You need to return a value that will close each window that has been called in the chain.

def main():
sg.theme('Tan Blue')

layout = [
    [sg.Text('Root Path:', size=(15, 1)), sg.InputText(root_path), sg.FolderBrowse()],
    [sg.Text('Date:', size=(15, 1)), sg.InputText(datetime.today().strftime('%Y-%m-%d %H:%M:%S')),
     sg.CalendarButton('Choose Date', target=(1, 1), key='date')],
    [sg.Output(size=(80, 15), key = '_output_')],
    [sg.Button('Run'), sg.Button('Exit'), sg.Button('Clear')]
]

window = sg.Window('App name', layout)

while True: # Event Loop
    event, values = window.Read()
    if event in (None, 'Exit'):
        break
    elif event == 'Run':
        to_continue = runScript(values) # Add a check if exit pressed
        if not to_continue:
            break
        window.Refresh()
    if event == 'Clear':
        window.FindElement('_output_').Update('')
window.close()   

def runScript(values):

...
 while True: # Event Loop
    event, values = window.Read()
    if event in (None, 'Exit'):
        to_continue = False
        window.close()
        return to_continue

Note that you would need to return to_continue as True if the user does not exit the window but instead the function finishes normally.

Jeff Mitchell
  • 1,067
  • 1
  • 8
  • 16