如何使用" RectangleSelector" TKinter中的Matplotlib小部件

时间:2017-07-28 11:32:11

标签: python python-3.x matplotlib tkinter tkinter-canvas

我正在开发一个项目,我必须绘制一些从传感器读取的数据,然后用户必须从图中选择一部分数据并提取它。我使用python3,tkinter用于gui和matplotlib用于绘图。我想" RectangleSelector" matplotlib的小部件适合这项工作。但是,虽然我检查了matplotlib引用提供的示例,但我无法设法让RectangleSelector小部件工作。你能帮我解决这个问题吗?

现在,当我尝试运行代码时,我得到:" AttributeError:' NoneType'对象没有属性' mpl_connect'"

代码如下所示:

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) --> (%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()

并且完整的错误报告如下:

Traceback (most recent call last):

  File "<ipython-input-340-63c18d2a9507>", line 1, in <module>
    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 <module>
    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'   

1 个答案:

答案 0 :(得分:0)

在您定义矩形选择器的位置,尚未定义图形画布(None),因此错误'NoneType' object has no attribute 'mpl_connect'

您需要在将图形设置为画布后定义矩形选择器。这可以确保您提供给矩形选择器的轴位于图形中,该图形具有画布集。

    canvas = FigureCanvasTkAgg(f, self)
    # ...
    toggle_selector.RS = RectangleSelector(a, line_select_callback, ..., interactive=True)
    # ...
    canvas.mpl_connect('key_press_event', toggle_selector)