Philipp
Philipp

Reputation: 333

How to disconnect matplotlibs event handler?

I am trying to find a way to disconnect matplotlib's event handler by using mpl_disconnect. So far I followed the instructions here and here to learn how to disconnect but unfortunately it did not work for me.

With the following code I am able to connect button_press_event to the callback function on_press by using a checkbutton. After unchecking cid prints 0 so it should be disconnected but the callback function still fires.

I am using python 3.7.4 and matplotlib 3.1.1.

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.image as mpimg    

class MainApplication(Tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        Tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        parent.iconify
        parent.grid_rowconfigure(1, weight=1)
        parent.grid_columnconfigure(1, weight=1)

        top_frame = Tk.Frame(parent)
        top_frame.grid(row=0)       
        mid_frame = Tk.Frame(parent)
        mid_frame.grid(row=1)

        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        self.ax.set_aspect('equal')       
        canvas = FigureCanvasTkAgg(self.fig, mid_frame)
        canvas.get_tk_widget().grid(row=0, column=0, sticky="nsew")
        canvas._tkcanvas.grid(row=0, column=0, sticky="nsew")        
        img = mpimg.imread('stinkbug.png')  # insert image file here
        self.ax.imshow(img)
        self.fig.canvas.draw()

        self.var1 = Tk.IntVar()
        chkbx1 = Tk.Checkbutton(top_frame, text='connect', variable=self.var1, command=self.de_activate)
        chkbx1.grid(row=0, column=0, sticky="w")

    def de_activate(self):
        print('checkbutton: '+str(self.var1.get()))
        self.cidpress = 0
        if self.var1.get() == 1:
            self.cidpress = self.fig.canvas.mpl_connect('button_press_event', self.on_press)
            print('on_press connected (cid='+str(self.cidpress)+')')
        else:
            self.fig.canvas.mpl_disconnect(self.cidpress)
            print('on_press disconnected (cid='+str(self.cidpress)+')')

    def on_press(self, event):
        if event.inaxes != self.ax: return
        print('button pressed')

if __name__ == '__main__':
    root = Tk.Tk()
    MainApplication(root).grid(row=0, column=0, sticky="nsew")
    root.mainloop()

Upvotes: 0

Views: 1470

Answers (1)

GPhilo
GPhilo

Reputation: 19153

To disconnect, you must pass the original cid to mpl_disconnect, but you're resetting self.cidpress before the if .. else .. block, so you're always requesting the disconnection of cid 0. Remove self.cidpress = 0 and place it right after self.fig.canvas.mpl_disconnect(self.cidpress):

def de_activate(self):
    print('checkbutton: '+str(self.var1.get()))
    if self.var1.get() == 1:
        self.cidpress = self.fig.canvas.mpl_connect('button_press_event', self.on_press)
        print('on_press connected (cid='+str(self.cidpress)+')')
    else:
        self.fig.canvas.mpl_disconnect(self.cidpress)
        self.cidpress = 0 # <<<<<<<<<<<<<<<<<<<<
        print('on_press disconnected (cid='+str(self.cidpress)+')')

Upvotes: 1

Related Questions