简体   繁体   English

PySimpleGUI。 运行时错误:主线程不在主循环中

[英]PySimpleGUI. RuntimeError: main thread is not in main loop

As a Python learner I decided to try out PySimpleGUI, and wrote a script in which the relevant snippet is:作为一名 Python 学习者,我决定尝试 PySimpleGUI,并编写了一个脚本,其中的相关代码段是:

`    
import PySimpleGUI as sg 
....
window = sg.Window('Output Filename Creator').Layout(layout)
while True:
    event, values = window.Read()
    if event is None  or event == "Cancel":
        window.Close()
        sys.exit()
    else:
        outfile = values['file']
        window.Close()
        return outfile    `  

I use Windows 10, Python 3.7, Idle 3.7, and PySimpleGUI-3.24.0.我使用 Windows 10、Python 3.7、Idle 3.7 和 PySimpleGUI-3.24.0。 After running the script that contains the snippet above (no execution errors) I go to the Idle shell and try to type in len('1').运行包含上述代码片段的脚本(无执行错误)后,我进入空闲 shell 并尝试输入 len('1')。 At entering the open bracket the following error is generated:在输入开放括号时,会生成以下错误:

Traceback (most recent call last): File "C:\\Users\\Paul\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\tkinter\\__init__.py", line 332, in __del__ if self._tk.getboolean(self._tk.call("info", "exists", self._name)): RuntimeError: main thread is not in main loop

I know that PySimpleGUI is based on tkinter, but that is as far as my knowledge goes.我知道 PySimpleGUI 是基于 tkinter 的,但据我所知,仅此而已。 I don't know how threading works in Python or how PySimpleGUI is interfaced with Tk.我不知道 Python 中的线程是如何工作的,也不知道 PySimpleGUI 是如何与 Tk 接口的。 Yet I would like to know where the error comes from and what I can do to avoid it.但是我想知道错误来自哪里以及我可以做些什么来避免它。

Update: the code reduced to bare essentials still gives the same error when window is closed by clicking on cross in upper right corner:更新:当通过单击右上角的十字关闭窗口时,简化为基本要素的代码仍然会出现相同的错误:

def OutputFileName(default):
    import PySimpleGUI as sg      

    layout = [
              [sg.In(default, key='file', size=(70,1)),
               sg.SaveAs('Browse')],
              [sg.Save(), sg.Text(' '*35), sg.Cancel()]
            ]

    window = sg.Window(' ').Layout(layout)
    event, values = window.Read()
    if event is None or event == "Cancel":
        return
    elif event == 'Save':
        return values['file']
outf = OutputFileName('foo.txt')

This problem is due to IDLE being written using tkinter.这个问题是由于 IDLE 是使用 tkinter 编写的。

tkinter is very picky about resources and threads. tkinter 对资源和线程非常挑剔。 There is a warning in the PySimpleGUI documentation about utilizing PySimpleGUI in a threaded environment because you can get into situations like this one where resources are freed in the incorrect thread or tkinter gets confused about who is running the mainloop. PySimpleGUI 文档中有关于在线程环境中使用 PySimpleGUI 的警告,因为您可能会遇到这样的情况:资源在错误的线程中被释放,或者 tkinter 对谁在运行主循环感到困惑。

Here is an older post that talks about problems running multiple mainloops when using IDLE.这是一篇较旧的帖子,讨论了在使用 IDLE 时运行多个主循环的问题。

https://groups.google.com/forum/#!topic/comp.lang.python/kr7lKj4qMl4 https://groups.google.com/forum/#!topic/comp.lang.python/kr7lKj4qMl4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM