pythonpysimplegui

Yes/No Popup on PySimpleGUI


How do I use yes/no popups on PySimpleGUI? I've searched everywhere, but the answers just aren't clear on this.

At the moment, I am just trying to make a simple yes/no program so I can get my head around this.

Here's my code so far:

import PySimpleGUI as sg
import time

sg.popup("Welcome to this random file.")
result = sg.popup_yes_no("Would you like to continue?")

if(result == "yes"):
    sg.popup("Great! You have chosen to continue the process.")

elif(result == "no"):
    sg.popup("Exiting process...")
    sg.popup("See you!")
    time.sleep(3)
    exit()

else:
    sg.popup("Invalid input")


Solution

  • You have to turn yes into Yes and no to No. You can find this by printing the result, like this:

    result = sg.popup_yes_no("Would you like to continue?")
    print(result)
    

    So the corrected code would be this:

    import PySimpleGUI as sg
    import time
    
    sg.popup("Welcome to this random file.")
    result = sg.popup_yes_no("Would you like to continue?")
    
    if(result == "Yes"):
        sg.popup("Great! You have chosen to continue the process.")
    
    elif(result == "No"):
        sg.popup("Exiting process...")
        sg.popup("See you!")
        time.sleep(3)
        exit()
    
    else:
        sg.popup("Invalid input")
    

    My advice is to always use some sort of debugging, maybe through debugging libraries or through print.