theFinestHobo
theFinestHobo

Reputation: 51

How to manually close a popup in PySimpleGui?

How do I manually close a popup in PySimpleGui? I know I can close it automatically after a certain time, but I want to close it manually. I tried following:

#popup to inform the user that the installtion starts
installingPopup=sg.popup_no_buttons('start installation...',non_blocking=True)
#actual installation
installation()
#close popup
installingPopup.close()

Upvotes: 0

Views: 1583

Answers (1)

Jason Yang
Jason Yang

Reputation: 13051

There's no way to close Popup defined by PySimpleGUI.

Maybe you need to define one by yourself, so you can have the variable window, and close it by window.close() latter.

Example Code for it

from time import sleep
import threading
import PySimpleGUI as sg

def installation(window, steps):
    step = 1
    while step <= steps:
        window.write_event_value('JOB', f'Step {step} of Installation ...')
        sleep(1)
        step += 1
    window.write_event_value('JOB DONE', None)

def popup(message):
    sg.theme('DarkGrey')
    layout = [[sg.Text(message)]]
    window = sg.Window('Message', layout, no_titlebar=True, keep_on_top=True, finalize=True)
    return window

sg.theme('DarkBlue3')
sg.Window._move_all_windows = True

layout = [
    [sg.Button('Install', tooltip='Installation')],
    [sg.Text('', size=50, key='STATUS')],
]
window = sg.Window('Matplotlib', layout, finalize=True)
pop_win = None
while True:

    event, values = window.read(timeout=10)

    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Install':
        window['Install'].update(disabled=True)
        popup_win = popup('Start installation...')
        window.force_focus()
        threading.Thread(target=installation, args=(window, 5), daemon=True).start()
    elif event == 'JOB':
        message = values['JOB']
        window['STATUS'].update(message)
    elif event == 'JOB DONE':
        popup_win.close()
        popup_win = None
        window['Install'].update(disabled=False)
        window['STATUS'].update("Installation done")

if popup_win:
    popup_win.close()
window.close()

enter image description here

Maybe it is not necessary to show a popup when installation, just message shown on a sg.StatusBar or a sg.Text in main window, it will be easier, all depend on your idea.

Upvotes: 2

Related Questions