PySimpleGUI. RuntimeError: main thread is not in main loop












0















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









share|improve this question















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











  • 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











  • 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
















0















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









share|improve this question















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











  • 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











  • 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














0












0








0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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











  • 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











  • 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













  • 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 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

















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












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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






    share|improve this answer




























      0














      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






      share|improve this answer


























        0












        0








        0







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 24 at 7:18









        MikeyBMikeyB

        78359




        78359
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy