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:
`
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. 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'). At entering the open bracket the following error is generated:
Traceback (most recent call last):
File "C:UsersPaulAppDataLocalProgramsPythonPython37-32libtkinter__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. I don't know how threading works in Python or how PySimpleGUI is interfaced with 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')
python multithreading
migrated from superuser.com Jan 21 at 15:54
This question came from our site for computer enthusiasts and power users.
|
show 5 more comments
As a Python learner I decided to try out PySimpleGUI, and wrote a script in which the relevant snippet is:
`
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. 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'). At entering the open bracket the following error is generated:
Traceback (most recent call last):
File "C:UsersPaulAppDataLocalProgramsPythonPython37-32libtkinter__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. I don't know how threading works in Python or how PySimpleGUI is interfaced with 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')
python multithreading
migrated from superuser.com Jan 21 at 15:54
This question came from our site for computer enthusiasts and power users.
It seems that you cannot go in the loop a second time as in both branches of theif statement
you exit the loop, so why the need forwhile True:
?
– gkivanov
Jan 21 at 16:23
The PySimpleGUI documentation gave me the impression that you must loop over events -- such as keyboard presses. Anyway, I took the loop out and the error still occurs.
– P. Wormer
Jan 21 at 16:38
As an addition to my previous comment: I modified an example in which sg.Window() was invoked with the argument 'return_keyboard_events=True'. I overlooked this argument and did not include it, obviating an infinite loop.
– P. Wormer
Jan 21 at 17:09
Remove thewindow.Close()
from thewhile True:
loop. Follow this pattern persistent-window-multiple-reads-using-an-event-loop-updates-data-in-window
– stovfl
Jan 21 at 19:21
Took Close() out of the loop (twice). Added 'do_not_clear=True' to In(...) as in link. Problem persists.
– P. Wormer
Jan 21 at 21:13
|
show 5 more comments
As a Python learner I decided to try out PySimpleGUI, and wrote a script in which the relevant snippet is:
`
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. 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'). At entering the open bracket the following error is generated:
Traceback (most recent call last):
File "C:UsersPaulAppDataLocalProgramsPythonPython37-32libtkinter__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. I don't know how threading works in Python or how PySimpleGUI is interfaced with 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')
python multithreading
As a Python learner I decided to try out PySimpleGUI, and wrote a script in which the relevant snippet is:
`
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. 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'). At entering the open bracket the following error is generated:
Traceback (most recent call last):
File "C:UsersPaulAppDataLocalProgramsPythonPython37-32libtkinter__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. I don't know how threading works in Python or how PySimpleGUI is interfaced with 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')
python multithreading
python multithreading
edited Jan 21 at 21:51
P. Wormer
asked Jan 21 at 15:40
P. WormerP. Wormer
1036
1036
migrated from superuser.com Jan 21 at 15:54
This question came from our site for computer enthusiasts and power users.
migrated from superuser.com Jan 21 at 15:54
This question came from our site for computer enthusiasts and power users.
It seems that you cannot go in the loop a second time as in both branches of theif statement
you exit the loop, so why the need forwhile True:
?
– gkivanov
Jan 21 at 16:23
The PySimpleGUI documentation gave me the impression that you must loop over events -- such as keyboard presses. Anyway, I took the loop out and the error still occurs.
– P. Wormer
Jan 21 at 16:38
As an addition to my previous comment: I modified an example in which sg.Window() was invoked with the argument 'return_keyboard_events=True'. I overlooked this argument and did not include it, obviating an infinite loop.
– P. Wormer
Jan 21 at 17:09
Remove thewindow.Close()
from thewhile True:
loop. Follow this pattern persistent-window-multiple-reads-using-an-event-loop-updates-data-in-window
– stovfl
Jan 21 at 19:21
Took Close() out of the loop (twice). Added 'do_not_clear=True' to In(...) as in link. Problem persists.
– P. Wormer
Jan 21 at 21:13
|
show 5 more comments
It seems that you cannot go in the loop a second time as in both branches of theif statement
you exit the loop, so why the need forwhile True:
?
– gkivanov
Jan 21 at 16:23
The PySimpleGUI documentation gave me the impression that you must loop over events -- such as keyboard presses. Anyway, I took the loop out and the error still occurs.
– P. Wormer
Jan 21 at 16:38
As an addition to my previous comment: I modified an example in which sg.Window() was invoked with the argument 'return_keyboard_events=True'. I overlooked this argument and did not include it, obviating an infinite loop.
– P. Wormer
Jan 21 at 17:09
Remove thewindow.Close()
from thewhile True:
loop. Follow this pattern persistent-window-multiple-reads-using-an-event-loop-updates-data-in-window
– stovfl
Jan 21 at 19:21
Took Close() out of the loop (twice). Added 'do_not_clear=True' to In(...) as in link. Problem persists.
– P. Wormer
Jan 21 at 21:13
It seems that you cannot go in the loop a second time as in both branches of the
if statement
you exit the loop, so why the need for while True:
?– gkivanov
Jan 21 at 16:23
It seems that you cannot go in the loop a second time as in both branches of the
if statement
you exit the loop, so why the need for while True:
?– gkivanov
Jan 21 at 16:23
The PySimpleGUI documentation gave me the impression that you must loop over events -- such as keyboard presses. Anyway, I took the loop out and the error still occurs.
– P. Wormer
Jan 21 at 16:38
The PySimpleGUI documentation gave me the impression that you must loop over events -- such as keyboard presses. Anyway, I took the loop out and the error still occurs.
– P. Wormer
Jan 21 at 16:38
As an addition to my previous comment: I modified an example in which sg.Window() was invoked with the argument 'return_keyboard_events=True'. I overlooked this argument and did not include it, obviating an infinite loop.
– P. Wormer
Jan 21 at 17:09
As an addition to my previous comment: I modified an example in which sg.Window() was invoked with the argument 'return_keyboard_events=True'. I overlooked this argument and did not include it, obviating an infinite loop.
– P. Wormer
Jan 21 at 17:09
Remove the
window.Close()
from the while True:
loop. Follow this pattern persistent-window-multiple-reads-using-an-event-loop-updates-data-in-window– stovfl
Jan 21 at 19:21
Remove the
window.Close()
from the while True:
loop. Follow this pattern persistent-window-multiple-reads-using-an-event-loop-updates-data-in-window– stovfl
Jan 21 at 19:21
Took Close() out of the loop (twice). Added 'do_not_clear=True' to In(...) as in link. Problem persists.
– P. Wormer
Jan 21 at 21:13
Took Close() out of the loop (twice). Added 'do_not_clear=True' to In(...) as in link. Problem persists.
– P. Wormer
Jan 21 at 21:13
|
show 5 more comments
1 Answer
1
active
oldest
votes
This problem is due to IDLE being written using tkinter.
tkinter is very picky about resources and threads. 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.
Here is an older post that talks about problems running multiple mainloops when using IDLE.
https://groups.google.com/forum/#!topic/comp.lang.python/kr7lKj4qMl4
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54293491%2fpysimplegui-runtimeerror-main-thread-is-not-in-main-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This problem is due to IDLE being written using tkinter.
tkinter is very picky about resources and threads. 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.
Here is an older post that talks about problems running multiple mainloops when using IDLE.
https://groups.google.com/forum/#!topic/comp.lang.python/kr7lKj4qMl4
add a comment |
This problem is due to IDLE being written using tkinter.
tkinter is very picky about resources and threads. 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.
Here is an older post that talks about problems running multiple mainloops when using IDLE.
https://groups.google.com/forum/#!topic/comp.lang.python/kr7lKj4qMl4
add a comment |
This problem is due to IDLE being written using tkinter.
tkinter is very picky about resources and threads. 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.
Here is an older post that talks about problems running multiple mainloops when using IDLE.
https://groups.google.com/forum/#!topic/comp.lang.python/kr7lKj4qMl4
This problem is due to IDLE being written using tkinter.
tkinter is very picky about resources and threads. 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.
Here is an older post that talks about problems running multiple mainloops when using IDLE.
https://groups.google.com/forum/#!topic/comp.lang.python/kr7lKj4qMl4
answered Jan 24 at 7:18
MikeyBMikeyB
78359
78359
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54293491%2fpysimplegui-runtimeerror-main-thread-is-not-in-main-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems that you cannot go in the loop a second time as in both branches of the
if statement
you exit the loop, so why the need forwhile True:
?– gkivanov
Jan 21 at 16:23
The PySimpleGUI documentation gave me the impression that you must loop over events -- such as keyboard presses. Anyway, I took the loop out and the error still occurs.
– P. Wormer
Jan 21 at 16:38
As an addition to my previous comment: I modified an example in which sg.Window() was invoked with the argument 'return_keyboard_events=True'. I overlooked this argument and did not include it, obviating an infinite loop.
– P. Wormer
Jan 21 at 17:09
Remove the
window.Close()
from thewhile True:
loop. Follow this pattern persistent-window-multiple-reads-using-an-event-loop-updates-data-in-window– stovfl
Jan 21 at 19:21
Took Close() out of the loop (twice). Added 'do_not_clear=True' to In(...) as in link. Problem persists.
– P. Wormer
Jan 21 at 21:13