1

我想将 添加matplotlib.backends.backends_wx.NavigationToolbar2Wx到我正在开发的 GUI 中,但我不想将它放在 上FigureCanvas,因为它会迫使我的布局变得相当尴尬。有没有办法将它添加NavigationToolbar2Wx到 GUI 的不同部分而不是它附加到的画布?特别是,我想要以下布局

+------------------------------------------------------------------------+
| +--------------------------------------------------------------------+ |
| |                                                                    | |
| |                                                                    | |
| |                                                                    | |
| |                                                                    | |
| |                            FigureCanvas                            | |
| |                                                                    | |
| |                                                                    | |
| +--------------------------------------------------------------------+ |
| +--------------------------------------------------------------------+ |
| |  TextCtrl   TextCtrl        NavToolbar           TextCtrl  Button  | |
| +--------------------------------------------------------------------+ |
|                                                                        |
|                               More GUI here                            |
|                                                                        |
+------------------------------------------------------------------------+

其中FigureCanvas和它下面的部分在同一垂直BoxSizer,与 的区域NavigationToolbar是水平的BoxSizer

我尝试了各种事情,例如明显构建工具栏,将大小设置为水平BoxSizer,并将其添加到BoxSizer,但它没有绘制(尽管InspectionTool会突出显示它应该在, and when I close the window it throws a segfault. If I don't set the sizer for the toolbar, the behavior is identical except it doesn't throw a segfault. I am making sure to call哪里 .Realize() and.Update( )` 在工具栏上,但到目前为止我还没有运气。

我想要做的甚至是可能的,还是我必须手动实现按钮和行为?

4

1 回答 1

0

扩展我的评论,这是一个几乎 :-) 工作示例,省略了琐碎的部分。我已经用作wx.lib.agw.buttonpanel我的工具栏,但粗略的其他任何东西也可以使用。

import wx.lib.agw.buttonpanel as BP
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx


#---------------------------- plot panel -----------------------------
class PanelWithCanvas(wx.Panel):
    """
    This panel contains plot figure and other things...
    """
    def __init__(self, parent, frame):
        wx.Panel.__init__(self, parent)

        self.frame = frame

        ...

        # create figure
        self.figure = plt.Figure()
        self.axes  = self.figure.add_subplot(111)
        self.axes.grid()
        # attach a canvas to the figure
        self.canvas = FigureCanvas(self, wx.ID_ANY, self.figure) 

        # create the default matplotlib toolbar and attach it to the canvas
        self.toolbar = NavigationToolbar2Wx(self.canvas)
        # realize and hide it, since it will be replaced by a better one
        self.toolbar.Realize()
        self.toolbar.Hide()

        # create a new plot toolbar to replace the default one
        self.ptb = plotToolbar(self)

        ...


#---------------------------- plot toolbar -----------------------------
class plotToolbar(BP.ButtonPanel):
    """
    Create small toolbar which is added to the main panel
    par:  parent
    """
    def __init__(self, par):
        BP.ButtonPanel.__init__(self, par)

        # PanelWithCanvas
        self.pwc = par

        self.ID_FIT = wx.NewId()
        self.ID_ZOOM = wx.NewId()
        self.ID_PAN = wx.NewId()
        self.ID_SAVE = wx.NewId()

        self.AddSpacer()

        btn1 = BP.ButtonInfo(self, self.ID_FIT, bitmaps['zoomfit'].GetBitmap(),
                         shortHelp='Fit plot extents')
        self.AddButton(btn1)
        self.Bind(wx.EVT_BUTTON, self.ZoomFit, btn1)

        self.btnzoom = BP.ButtonInfo(self, self.ID_ZOOM, bitmaps['zoom'].GetBitmap(),
                         kind=wx.ITEM_CHECK, shortHelp='Zoom in/out')
        self.AddButton(self.btnzoom)
        self.Bind(wx.EVT_BUTTON, self.Zoom, self.btnzoom)

        self.btnpan = BP.ButtonInfo(self, self.ID_PAN, bitmaps['pan'].GetBitmap(),
                         kind=wx.ITEM_CHECK, shortHelp='Move plot')
        self.AddButton(self.btnpan)
        self.Bind(wx.EVT_BUTTON, self.Pan, self.btnpan)

        btnsav = BP.ButtonInfo(self, self.ID_SAVE, bitmaps['saveimage'].GetBitmap(),
                         shortHelp='Save image')
        self.AddButton(btnsav)
        self.Bind(wx.EVT_BUTTON, self.SaveImage, btnsav)

        self.AddSpacer()

        self.DoLayout()

    def ZoomFit(self, event):
        self.pwc.toolbar.home()
        event.Skip()

    def Zoom(self, event):
        self.pwc.toolbar.zoom()
        event.Skip()

    def Pan(self, event):
        self.pwc.toolbar.pan()
        event.Skip()

    def SaveImage(self, event):
        self.pwc.toolbar.save_figure(None)
        event.Skip()

请注意,您必须自己管理按钮的行为,因为其中一些按钮是仅推送(适合所有​​,保存),一些是切换(缩放,平移)并且是互斥的......或者您可以只需将它们全部保留为按钮,如NavigationToolbar2Wx. 希望这可以帮助。

于 2013-11-06T14:04:37.247 回答