首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何自动关闭PySimpleGUI弹出窗口?

如何自动关闭PySimpleGUI弹出窗口?
EN

Stack Overflow用户
提问于 2020-09-14 16:23:38
回答 2查看 2.4K关注 0票数 0

我正在使用PySimpleGUI并打开自动关闭弹出窗口,但它不会自动关闭,即使我按下OK按钮。只有当我按下“X”键时

下面是我的代码:

代码语言:javascript
复制
import PySimpleGUI as sg
import threading
import time

layout = [
    [sg.Text('', size=(40, 1))],
    [sg.Text('', size=(30, 2)), sg.Text('Press "Start" button', size=(55, 12), key='-MAIN-')],
    [sg.Button('Start', size=(10,2))],
]
window = sg.Window('APP', layout)
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == 'Start':
        def thread_reminder(seconds):
            seconds = 0
            while True:
                seconds += 1
                time.sleep(1)
                print(seconds)
                if seconds == 10:
                    sg.popup_auto_close("1 minute passed")
        threading.Thread(target=thread_reminder, args=(1,), daemon=True).start()
window.close()

这给了我这个错误或异常,我不知道:

代码语言:javascript
复制
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/User/Downloads/jkl;'.py", line 23, in thread_reminder
    sg.popup_auto_close("1 minute passed")
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 15782, in PopupAutoClose
    return Popup(*args, title=title, button_color=button_color, background_color=background_color, text_color=text_color,
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 15353, in Popup
    button, values = window.read()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 7568, in Read
    results = self._read(timeout=timeout, timeout_key=timeout_key)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 7623, in _read
    self._Show()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 7395, in _Show
    StartupTK(self)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 12817, in StartupTK
    window.TKroot.mainloop()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1420, in mainloop
    self.tk.mainloop(n)
RuntimeError: Calling Tcl from different apartment
*** Faking timeout ***

*** Faking timeout ***意味着popup必须关闭,但它没有关闭。

也许是因为线程化

请帮帮我!

EN

回答 2

Stack Overflow用户

发布于 2021-06-22 00:21:00

不要在另一个线程中调用PySimpleGUI/tkinter的GUI方法。使用window.write_event_value生成一个事件并在事件循环中处理它。

修订后的代码

代码语言:javascript
复制
import PySimpleGUI as sg
import threading
import time

def thread_reminder(seconds, window):
    count = 0
    while count < seconds :
        count += 1
        time.sleep(1)
        print(count)
    window.write_event_value('Alarm', "1 minute passed")

layout = [
    [sg.Text('', size=(40, 1))],
    [sg.Text('', size=(30, 2)), sg.Text('Press "Start" button', size=(55, 12), key='-MAIN-')],
    [sg.Button('Start', size=(10,2))],
]
window = sg.Window('APP', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == 'Start':
        threading.Thread(target=thread_reminder, args=(10, window), daemon=True).start()
    elif event == 'Alarm':
        message = values[event]
        sg.popup_auto_close(message)

window.close()
票数 1
EN

Stack Overflow用户

发布于 2021-06-21 19:11:00

有两种解决方案:

  1. PySimpleGUI提供了在给定时间后关闭的弹出窗口:有关详细信息,请参阅参考::sg.popup_timed('popup_timed') # Automatically closes sg.popup_auto_close('popup_auto_close') # Same as PopupTimed

  1. 您可以在事件循环中创建一个更新间隔。完整的循环应该如下所示:

代码语言:javascript
复制
while True:
    event, values = window.Read(timeout = 1000 * 10)  # in milliseconds

    if event in ('__TIMEOUT__',):
        print('timed execution inside event loop')
        ### put you window closing commands here ###

    if event in (sg.WIN_CLOSED,): break
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63880649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档