Pysimplegui: [Question/Bug] My problem is that I get exceptions that I cannot explain

Created on 16 Oct 2019  路  12Comments  路  Source: PySimpleGUI/PySimpleGUI

Type of Issues (Question)

My problem is that I get exceptions that I cannot explain

Operating System

Debian 10 (buster)

Python version

3.7.3

PySimpleGUI Port and Version

4.4.1

Your Experience Levels In Months or Years

2 years Python programming experience
2 years Programming experience overall
no - Have used another Python GUI Framework (tkiner, Qt, etc) previously?

You have completed these steps:

  • [x] Read instructions on how to file an Issue
  • [x] Searched through main docs http://www.PySimpleGUI.org for your problem
  • [x] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [x] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • [x] Note that there are also Demo Programs under each port on GitHub
  • [x] Run your program outside of your debugger (from a command line)
  • [x] Searched through Issues (open and closed) to see if already reported

Code or partial code causing the problem

EXCEPTION LIST

Exception ignored in: <function Image.__del__ at 0x7f75cf76ed90>
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 3507, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
RuntimeError: main thread is not in main loop
Exception ignored in: <function Variable.__del__ at 0x7f75cf7d1c80>
Traceback (most recent call last):
  File "/usr/lib/python3.7/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
RuntimeError: main thread is not in main loop
Exception ignored in: <function Variable.__del__ at 0x7f75cf7d1c80>
Traceback (most recent call last):
  File "/usr/lib/python3.7/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
Tcl_AsyncDelete: async handler deleted by the wrong thread

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

All 12 comments

You're running something multithreaded.

You cannot make any PySimpleGUI calls from a thread.

https://pysimplegui.readthedocs.io/en/latest/#do-not-attempt-to-call-pysimplegui-from-multiple-threads-its-tkinter-based-and-tkinter-has-issues-with-multiple-threads

Our app is multithreaded, but we don't do any calls from a thread to the gui.

You don't do any updates to things in the Window in your threads?

Somehow tkinter is getting called from a thread that is not the main thread.

Sometimes this is caused by objects being deleted in another thread that have some variable or reference to a PySimpleGUI object. When the delete happens in another thread, it calls the tkinter delete code which is what is testing to see if the correct thread is running.

What happens is:

  • the program is started
  • PySimpleGUI gets imported as 'gui'
  • 'gui' gets passed onto a function A inside a class instance
  • concurrent.futures.ThreadPoolExecutor gets called to run threads on class function B inside mentioned function A, but 'gui' is NOT used/called inside threaded function B

yea, that is sort of what I thought. What happens if you remove passing in this reference to the GUI? there needs to be a real firewall between threads and the GUI with no sharing of data, even if the data is not directly referenced, between the two pieces of code

I implemented your suggested changes by

  • removing any passing of the GUI as parameters
  • inside the class object, any GUI import has been removed
  • extracted any function, which required the GUI, from the class instance into a separate script, which now gets imported upon call

The error continues to persist.

Damn, that's really sucky.

I'm exploring ways to perform cleanup better on the windows when they are closed.

I'm not 100% sure, but the problem seems to me to be one of Python garbage collect. Stuff is getting around to being deleted a ways after the PySimpleGUI stuff has completed. The result is that the garbage collection, the delete routines withing tkinter are being executed in a different thread than the main thread.

If you search on this error, you'll find it goes WAY back, not just in terms of PySimpleGUI, but many years back. I thought I posted an announcement or an issue somewhere that contained a link or information about this error.

I'm working on this actively.

Can you try deleting the window object when you're done with it? I assume you're only running a single window and not using popups. Popups would need to get deleted too which you won't be able to do.

Anyway, I'll get some experimental code up when I find something.

I don't suppose you have a short piece of code that will demonstrate this?

Can I ask how many windows you open or what popup or debug print calls you make?

If you are making and showing a single window that you close prior to running the code that is generating errors, then you can delete the window, assuming it's held in the variable named window, with this line of code:

del window

Thank you very much for your engagement, I really appreciate it!
I have implemented the following, very simple change thanks to your suggestion:

# window.Close()
del window

This seems to have fixed the issue. I hope, that it also helps you to eradicate the error.

Oh wow.... WOW... like WOW!!!

Thank you very much for sticking in there with me on this. I've modified the code so that a window close will also delete the window object. I'm hoping that will solve a lot of these problems.

WHEW! What a relief.

Please do BOTH the close and the delete.... it should be:

window.Close()
del window

It's important as the close does some calls to tkinter that will not be made if you only delete the object. There are counts for the number of windows open for example.

Please let me know if doing both does NOT work for you.

The combination works great for me!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scmanjarrez picture scmanjarrez  路  5Comments

MikeTheWatchGuy picture MikeTheWatchGuy  路  6Comments

xuguojun168 picture xuguojun168  路  3Comments

OPMUSER picture OPMUSER  路  5Comments

mozesa picture mozesa  路  4Comments