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

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

我正在使用PySimpleGUI并打开自动关闭弹出窗口,但是即使按OK按钮,它也不会自动关闭。只有按'X'
这是我的代码

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

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

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 ***表示必须关闭弹出窗口,但不是。
也许是因为线程
请帮忙!

解决方法

对此有两种解决方案:

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

  2. 您在事件循环中创建更新间隔。完整的循环应如下所示:

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
,

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

修改后的代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?