title: How to use "RectangleSelector " widget of Matplotlib in TKinter
body: <p>I am working on a project where i have to plot some data which is read from a sensor then the user must select a part of this data from the plot and extract it. I am using python3, tkinter for gui and matplotlib for plotting. I thought "RectangleSelector" widget of matplotlib is suitable for this job. However, I couldn't manage to get RectangleSelector widget work eve though I examine the example provided by the matplotlib reference. Can you help me with this?</p>
  
  <p>Right now when I try to run the code I get: "AttributeError: 'NoneType' object has no attribute 'mpl_connect'"</p>
  
  <p>The code looks like this:</p>
  
  <pre><code>import matplotlib
  matplotlib.use("TkAgg")
  from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  from matplotlib.widgets import RectangleSelector
  from matplotlib.figure import Figure
  
  from matplotlib import style
  style.use('ggplot')
  
  import tkinter as tk
  
  import numpy as np
  
  #import AnalogRead
  
  LARGE_FONT= ("Verdana", 12)
  SAMPLE_MAX = 100000.0 # Maximum sampling rate
  SAMPLE = 1000.0 # sampling rate
  SAMPLE_SIZE = 2000 # self explaining
  LENGTH = SAMPLE_SIZE*10
  
  f = Figure(figsize=(10,5), dpi=100)#plt.figure(figsize=(10,5), dpi=100)#   
  a = f.add_subplot(111)
  
  time = np.zeros(1)
  data = np.zeros(1)
  
  
  def line_select_callback(eclick, erelease):
      'eclick and erelease are the press and release events'
      x1, y1 = eclick.xdata, eclick.ydata
      x2, y2 = erelease.xdata, erelease.ydata
      print("(%3.2f, %3.2f) --&gt; (%3.2f, %3.2f)" % (x1, y1, x2, y2))
      print(" The button you used were: %s %s" % (eclick.button, erelease.button))
  
  
  def toggle_selector(event):
      print(' Key pressed.')
      if event.key in ['Q', 'q'] and toggle_selector.RS.active:
          print(' RectangleSelector deactivated.')
          toggle_selector.RS.set_active(False)
      if event.key in ['A', 'a'] and not toggle_selector.RS.active:
          print(' RectangleSelector activated.')
          toggle_selector.RS.set_active(True)
  
  toggle_selector.RS = RectangleSelector(a, line_select_callback,
                                         drawtype='box',
                                         button=[1, 3],  # don't use middle button
                                         minspanx=5, minspany=5,
                                         spancoords='pixels',
                                         interactive=True)
  
  
  
  class appGUI(tk.Tk):
      def __init__(self, *args, **kwargs):
  
          tk.Tk.__init__(self, *args, **kwargs)
  
          #tk.Tk.iconbitmap(self, default="clienticon.ico")
          tk.Tk.wm_title(self, "Autocorrelation")
  
  
          container = tk.Frame(self)
          container.pack(side="top", fill="both", expand = True)
          container.grid_rowconfigure(0, weight=1)
          container.grid_columnconfigure(0, weight=1)
  
          self.frames = {}
          frame = mainFrame(container, self)
          self.frames[mainFrame] = frame
          frame.grid(row=0, column=0, sticky="nsew")
          self.show_frame(mainFrame)
  
      def show_frame(self, cont):
  
          frame = self.frames[cont]
          frame.tkraise()
  
  class mainFrame(tk.Frame):
  
  
      def __init__(self, parent, controller):
  
          global data, time, f, a, SAMPLE
  
          tk.Frame.__init__(self, parent)
          label = tk.Label(self, text="Graph Page!", font=LARGE_FONT)
          label.pack(pady=10,padx=10)
  
  
          #(time, data) = AnalogRead.Read(SAMPLE, SAMPLE_SIZE)
  
  
          a.plot([1,5,8],[4,2,7])        
  
          canvas = FigureCanvasTkAgg(f, self)
          canvas.show()
          canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
  
          toolbar = NavigationToolbar2TkAgg(canvas, self)
          toolbar.update()
          canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
  
          canvas.mpl_connect('key_press_event', toggle_selector)
  
          #self._update(SAMPLE, SAMPLE_SIZE, LENGTH)
  
      def _setRate(self,val):
          global SAMPLE
          SAMPLE = float(val)
  
      def _setSize(self,val):
          global SAMPLE_SIZE
          SAMPLE_SIZE = int(val)
  
  
  
  
  if __name__ == "__main__":
      app = appGUI()         
      app.mainloop()
  </code></pre>
  
  <p>and full error report is below:</p>
  
  <pre><code>Traceback (most recent call last):
  
    File "&lt;ipython-input-340-63c18d2a9507&gt;", line 1, in &lt;module&gt;
      runfile('C:/Users/AutoCorrelation/Documents/autocorrelation/lol.py', wdir='C:/Users/AutoCorrelation/Documents/autocorrelation')
  
    File "C:\Users\AutoCorrelation\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
      execfile(filename, namespace)
  
    File "C:\Users\AutoCorrelation\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
      exec(compile(f.read(), filename, 'exec'), namespace)
  
    File "C:/Users/AutoCorrelation/Documents/autocorrelation/lol.py", line 60, in &lt;module&gt;
      interactive=True)
  
    File "C:\Users\AutoCorrelation\Anaconda3\lib\site-packages\matplotlib\widgets.py", line 1744, in __init__
      state_modifier_keys=state_modifier_keys)
  
    File "C:\Users\AutoCorrelation\Anaconda3\lib\site-packages\matplotlib\widgets.py", line 1144, in __init__
      self.connect_default_events()
  
    File "C:\Users\AutoCorrelation\Anaconda3\lib\site-packages\matplotlib\widgets.py", line 1179, in connect_default_events
      self.connect_event('motion_notify_event', self.onmove)
  
    File "C:\Users\AutoCorrelation\Anaconda3\lib\site-packages\matplotlib\widgets.py", line 129, in connect_event
      cid = self.canvas.mpl_connect(event, callback)
  
  AttributeError: 'NoneType' object has no attribute 'mpl_connect'   
  </code></pre>