Search code examples
pythonopencvmatplotlibtkintervideo-processing

_tkinter.TclError: can't invoke “update” command: application has been destroyed error on pyplot.pause()


I'm working on serial data from arduino and processing the data in python using opencv2 and matplotlib library. Everything just works fine but everytime I close the figure window I got the error while I dont even use tkinter library.

Traceback (most recent call last):
  File "C:\Users\LENOVO\Documents\Arduino\project\coba.py", line 57, in <module>
    plt.pause(0.2)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\pyplot.py", line 438, in pause
    canvas.start_event_loop(interval)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\backend_bases.py", line 2407, in start_event_loop
    self.flush_events()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\backends\_backend_tk.py", line 390, in flush_events
    self._master.update()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1305, in update
    self.tk.call('update')
_tkinter.TclError: can't invoke "update" command: application has been destroyed

I dont even declare tkinter library,

import cv2
import serial
import numpy as np
import matplotlib.pyplot as plt

the use of matplotlib here is to show video from camera and serial data output side by side in realtime and the app is supposed to close when I press 'q'.

#create two subplots
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)

#create two image plots
im1 = ax1.imshow(grab_frame(video))
im2 = plt.imshow(cap2)
while True:
        #read from serial
        buffer = port.readline()
        temper = buffer.decode('utf-8')
        temper = temper.reshape(8,8)
        
        #update data
        im1.set_data(grab_frame(video))
        im2.set_data(temper)
        plt.pause(0.2)

        #stopper
        key = cv2.waitKey(1)
        if(key == ord('q')):
                break

plt.show()
video.release()
cv2.destroyAllWindows()

should I use tkinter library?


Solution

  • I've tried some ways to get over it and still i dont get where this has been done with tkinter, But IMO it's about I can't close the program when it is paused. So, instead of I put the stopper at bottom, I put it above the plt.pause(). this gonna looks something like this:

    #update data
    
    im1.set_data(grab_frame(video))
    im2.set_data(temper)
    #stopper
    key = cv2.waitKey(1)
    if(key == ord('q')):
            break
    plt.pause(0.2)