'How can pysimplegui give a popup message if the script has crashed?

I'm setting up a program using the pysimplegui Interface.

The program is based on user input. If the input is in a wrong format, or misspelled, the script will crash.

By now, when the script crashes, the GUI stays open with no indications that something is wrong.

Is there a way to display a popup warning and tell the user to exit the interface and restart the script?



Solution 1:[1]

[EDIT 2022] Just got a notice about this question... Like all of my SO answers, they get outdated pretty quickly.

For projects on GitHub, please open issues on the project's page so that the developers/experts can help and that you'll get the latest and greatest information.

My opinion on development.... Googling, and coming to StackOverflow will get you a quick answer, that may even run, but it is an inferior answer compared to reading the documentation or asking the development team. It's old information, often many years old.

Since posting the original answer that is at the bottom, there is a newer popup call that's specifically for this kind of problem. This popup is used within PySimpleGUI for error reporting. The traceback information is not only included in the error, but there's a button to open your editor to the line of code with the error. There's also an option to simply kill the application so that you can get busy fixing your problem.

The call is popup_error_with_traceback, was added in June 2021, and has parameters similar to the other PySimpleGUI popup calls. You'll even get an emoji to help lighten the mood

enter image description here

enter image description here

import PySimpleGUI as sg

layout = [  [sg.Text('My Window')],
            [sg.Input(key='-IN-'), sg.Text(size=(12,1), key='-OUT-')],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

try:
    while True:             # Event Loop
        event, values = window.read()
        window.bad()
        print(event, values)
        if event in (None, 'Exit'):
            break
        if event == 'Go':
            window['-OUT-'].update(values['-IN-'])
    window.close()
except Exception as e:
    sg.popup_error_with_traceback(f'An error happened.  Here is the info:', e)


OLD answer from 2020 below...

Lately, I've been adding larger scale crash handling for scripts that run in the system tray, or are widgets that run continuously. These kinds of programs don't have a system console.

This example shows 2 windows when a problem happens. One is an "error popup" the other prints to the "Debug window". The advantage of using the debug window is that you can copy and paste from it. You could choose one of the other depending on your preference.

import traceback
import PySimpleGUI as sg

layout = [  [sg.Text('My Window')],
            [sg.Input(key='-IN-'), sg.Text(size=(12,1), key='-OUT-')],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

try:
    while True:             # Event Loop
        event, values = window.read()
        window.bad()
        print(event, values)
        if event in (None, 'Exit'):
            break
        if event == 'Go':
            window['-OUT-'].update(values['-IN-'])
    window.close()
except Exception as e:
    tb = traceback.format_exc()
    sg.Print(f'An error happened.  Here is the info:', e, tb)
    sg.popup_error(f'AN EXCEPTION OCCURRED!', e, tb)

enter image description here

enter image description here

Solution 2:[2]

The following code will check whether the given input is a number or not and giving a opup saying (number or not an number) :

import PySimpleGUI as sg
layout = [[sg.Text('Check if number or not')],
          [sg.Input()],
          [sg.Button('Check'), sg.Button('Exit')]]
window = sg.Window('Checker', layout)
while True:
    event, values = window.read()
    if event in  (None, 'Exit'):
        break
    if event == 'Check':
        try:
            int(values[0])
            sg.Popup('It is a number')
        except:
            sg.Popup('Not a number')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Abdul Moiz