Philipp
Philipp

Reputation: 323

How to connect and disconnect matplotlib's event handler by using another class?

I am currently working on a program where the class MainApplication creates a GUI and loads an image. A second class Func includes functions that connect or disconnect matplotlib's event handler and a callback function on_press.

One issue is that I am using MainApplication's class objects self.fig and self.ax in the class Func. But I only get a TypeError: init() missing 2 required positional arguments: 'fig' and 'ax'.

How can I manipulate a class' object from another class in general?

MainApplication.py

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  
from Func import Func  

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')
        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):
        fc = Func()
        fc.__init__(self.fig, self.ax)
        print('checkbutton: '+str(self.var1.get()))
        if self.var1.get() == 1:
            fc.connect()
            print('on_press connected (cid='+str(self.cidpress)+')')
        else:
            fc.disconnect()
            print('on_press disconnected (cid='+str(self.cidpress)+')')

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

Func.py

class Func():
    def __init__(self, fig, ax):
        self.fig = fig
        self.ax = ax

    def connect(self):
        self.cidpress = self.fig.canvas.mpl_connect('button_press_event', self.on_press)

    def disconnect(self):
        self.fig.canvas.mpl_disconnect(self.cidpress)

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

Upvotes: 0

Views: 559

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

A correct version of this would look like

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  
#from Func import Func  

class Func():
    def __init__(self, fig, ax):
        self.fig = fig
        self.ax = ax

    def connect(self):
        self.cidpress = self.fig.canvas.mpl_connect('button_press_event', self.on_press)

    def disconnect(self):
        self.fig.canvas.mpl_disconnect(self.cidpress)

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

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.fc = Func(self.fig, self.ax)

        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('house.png')
        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()))
        if self.var1.get() == 1:
            self.fc.connect()
            print('on_press connected (cid='+str(self.fc.cidpress)+')')
        else:
            self.fc.disconnect()
            print('on_press disconnected (cid='+str(self.fc.cidpress)+')')

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

Upvotes: 1

Related Questions