aegirhall / console-menu

A simple Python menu system for building terminal user interfaces.
MIT License
363 stars 59 forks source link

Tcl_AsyncDelete Error - Multithreading and Matplotlib #21

Open jah-mann opened 5 years ago

jah-mann commented 5 years ago

Hello,

I am using the console menu package to create a menu of data analytics options, some of which call plotting functions (such as plotting histograms) with Matplotlib. I am able to call my functions and have the plots display correctly, but when I finally finish the entire program (after plotting, exiting the console menu, and exiting my program itself), I receive this error:

Exception ignored in: <function Image.del at 0x0000018B92C749D8> Traceback (most recent call last): File "C:\Users\JacobMann\AppData\Local\Programs\Python\Python37\lib\tkinter__init.py", line 3507, in del__ self.tk.call('image', 'delete', self.name) RuntimeError: main thread is not in main loop Tcl_AsyncDelete: async handler deleted by the wrong thread

Upon looking into this and finding various questions asked in the past about this problem (such as this one), it appears that TKinter is used for Matplotlib when graphing things, and it is intended to be run on a single thread. So whenever I call Matplotlib from one of the FunctionItems in the console menu, it ends up causing this error at the end of the program. I can confirm that this does not happen when I call the function outside of the console menu framework.

Previous solutions to this problem seem to be centered around importing Matplotlib and then doing matplotlib.use('Agg'), but this makes the back-end non-GUI based, so the solution does not work for people who wish to graph in Matplotlib.

Could this have to do with the "import threading" statement in the console menu code? Here are some posts I found on potentially getting rid of this problem: Deleting the TCL interpreter Running GUI on Main Thread

jah-mann commented 5 years ago

Other tests I have done:

1. Start my program > start console menu > make selections but do not plot anything > leave console menu > exit my program. ---- Result: No error message

2. Start my program > start console menu > make selections but do not plot anything > leave console menu > call script using Matplotlib directly from my code > exit my program. ---- Result: No error message.

So it seems as if the console menu and Matplotlib can each run at separate times within the same larger program and not have this issue. The issue only occurs when the console menu is currently running and Matplotlib is called within it.