【发布时间】:2020-09-14 08:23:37
【问题描述】:
我正在使用 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 *** 表示必须关闭弹出窗口,但没有。
也许是因为线程
请帮忙!
【问题讨论】:
-
@Karthik 我认为不是,因为它是关于 Tkinter,而不是 PySimpleGUI。其次,没有解释,所以我不知道如何从线程中删除它......
标签: python multithreading popup pysimplegui