Creating Matplotlib Toolbar in Python Traits Editor
Question: I'm working on a QT4 Traits-GUI in Python using Matplotlib for image display,
and so far I have [this](https://mail.enthought.com/pipermail/enthought-
dev/2012-May/031065.html) editor modified in an attempt to include a toolbar.

    
    
    from traitsui.qt4.editor import Editor
    from pyface.qt import QtGui, QtCore
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
    
    class _MPLFigureEditor(Editor):
    """
    Building custom figure editor for displaying matplotlib figures with a qt4-based Traits interface
    """
        scrollable  = True
    
        def init(self, parent):
            self.control = self._create_canvas(parent)
            self.set_tooltip()
    
        def update_editor(self):
            pass
    
        def _create_canvas(self, parent):
            mpl_canvas = FigureCanvas(self.value) #self.value is the figure linked to the editor factory
            mpl_toolbar = NavigationToolbar(mpl_canvas,???)
            layout = QtGui.QVBoxLayout()
            layout.addWidget(mpl_toolbar)
            layout.addWidget(mpl_canvas)
            return layout
    

Is this the correct place to create the toolbar? What would be second argument
to NavigationToolbar in this case? I'm trying to follow the examples I've
found on adding toolbars as QWidgets to figures, but these all create a
main_frame within a QMainWindow-based class, not a Traits Editor. Thanks in
advance.


Answer: Found the answer. Since `parent` is a `QGridLayout` itself, we can add the
toolbar widget directly to the layout in row 2, column 1 (to place it below
the canvas, which I believe is added as a widget in the view-editor creation).

    
    
    from traitsui.qt4.editor import Editor
    from pyface.qt import QtGui, QtCore
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
    
    class _MPLFigureEditor(Editor):
    """
    Building custom figure editor for displaying matplotlib figures with a qt4-based Traits interface
    """
        scrollable  = True
    
        def init(self, parent):
            self.control = self._create_canvas(parent)
            mpl_toolbar = NavigationToolbar(self.control,None)
            parent.addWidget(mpl_toolbar,2,1)
            self.set_tooltip()
            return
    
        def update_editor(self):
            pass
    
        def _create_canvas(self, parent):
        """ Create the MPL canvas. """
            mpl_canvas = FigureCanvas(self.value)
            return mpl_canvas
    

Image zoom/pan does seem to run a bit slow, but that could be another issue
altogether.

