【问题标题】:How to disconnect matplotlibs event handler?如何断开 matplotlibs 事件处理程序?
【发布时间】:2019-10-10 17:32:00
【问题描述】:

我正在尝试使用mpl_disconnect 来断开matplotlib 的事件处理程序。到目前为止,我按照说明 herehere 学习了如何断开连接,但不幸的是它对我不起作用。

使用下面的代码,我可以使用检查按钮将button_press_event 连接到回调函数on_press。取消选中cid 后会打印0,因此应该断开连接,但回调函数仍会触发。

我正在使用 python 3.7.4 和 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()

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

    要断开连接,您必须将原始cid 传递给mpl_disconnect,但您在if .. else .. 块之前重置self.cidpress,因此您始终请求断开cid 0。删除self.cidpress = 0,放在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)+')')
    

    【讨论】:

    • 这太容易了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2013-06-21
    • 1970-01-01
    • 2013-12-24
    • 2015-11-24
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多