6

When I thread matplotlib and I dont close the graph window frequently (by mouse - close window button) I get the following error:

Exception RuntimeError: RuntimeError('main thread is not in main loop',) in <bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at 0x7ff408080998>> ignored Exception RuntimeError: RuntimeError('main thread is not in main loop',) in <bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at 0x7ff3fbe373f8>> ignored
                                                                   Tcl_AsyncDelete: async handler deleted by the wrong thread
                                                                                                                             Aborted

What I do: I have a main loop calling methods of an instance. The matplotlib method is threaded by the init function of that instance. I cant call matplotlib as method inside the instance, I don't know why, so I thread it via __init__:

def __init__(self):
....
thread = threading.Thread(target=self.drawcharts, args=())
        thread.daemon = False                            
        thread.start()

the threaded method:

def drawcharts(self):

        global endthread

        ..do some math..

        try:

            plt.plot(k)
            plt.plot(x1)
            plt.plot(x2)
            plt.plot(x3)
            #plt.plot(v)
            plt.ylabel('some numbers')

            plt.show(block=True)
            self.drawcharts();
        except:
            self.drawcharts()


        if endthread==1:
            return


        return

The global variable endthread is triggered by the exit function of my program (keyboard exception).

I really need matplotlib to run independently from the rest of the program, as it is a curses program and I cant change the structure, so that matplotlib can run in non-blocking mode or else. Also I need the blocking mode as the widgets of the plot-window are needed.

I suppose to loop over the same matplotlib window over and over - without closing the plot windows sometimes - makes something overflow in matplotlib. For some time it works like a charm, but then exits ungracefully.

What can i do about it? Thanks for any help!

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
Peter Fleix
  • 113
  • 9

1 Answers1

-1

You should look into the questions and answers about making matplotlib.show() non-blocking.

For example, this one : Plotting in a non-blocking way with Matplotlib

This answer could do what you want in a simpler way ? https://stackoverflow.com/a/42674363/3898873