Python Forum
PySimpleGUI Try Except - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: PySimpleGUI Try Except (/thread-35574.html)



PySimpleGUI Try Except - jamesaarr - Nov-18-2021

Hi all,

I'm trying to get my program to try to make a layout with an sg.Image element, however the file path is a variable so it can update the image in the window based on the user input. However I need a way that it doesn't crash when the image isnt in the folder. I've tried putting try and except statements in several places but wondered if there's another way?

Thanks,
James


RE: PySimpleGUI Try Except - jamesaarr - Nov-18-2021

Hi all,

I found the solution in the end :

try:
    imageView['imageview'].update(filename = (r'your path\\yourimage'+".jpg"))
                            
    while True:             
        event,values = imageView.read()
        if event == sg.WIN_CLOSED:
            break
except:
        sg.popup("Image not found")