【问题标题】:How to manually close a popup in PySimpleGui?如何在 PySimpleGui 中手动关闭弹出窗口?
【发布时间】:2021-12-09 00:17:05
【问题描述】:

如何在 PySimpleGui 中手动关闭弹出窗口? 我知道我可以在一段时间后自动关闭它,但我想手动关闭它。我尝试了以下操作:

#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()

【问题讨论】:

    标签: python pysimplegui


    【解决方案1】:

    PySimpleGUI 定义的 Popup 无法关闭。

    也许你需要自己定义一个,这样你就可以拥有变量window,然后用window.close()关闭它。

    示例代码

    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()
    

    也许安装时不需要弹出弹窗,只在主窗口的sg.StatusBar或sg.Text上显示消息,会更容易,全看你的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多