具有多个面板的 wxPython 中的 Matplotlib

Matplotlib in wxPython with multiple panels

我正在尝试将此示例转换为多面板示例。

import os
import wx
import numpy as np
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.figure as figure
import matplotlib.backends.backend_wxagg as wxagg


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Title')
        #self.create_menu()
        self.create_main_panel()
        self.draw_figure()

    def create_menu(self):
        self.menubar = wx.MenuBar()

        menu_file = wx.Menu()
        m_exit = menu_file.Append(-1, "&Quit\tCtrl-Q", "Quit")
        self.Bind(wx.EVT_MENU, self.on_exit, m_exit)
        self.menubar.Append(menu_file, "&File")
        self.SetMenuBar(self.menubar)

    def create_main_panel(self):
        """ Creates the main panel with all the controls on it:
             * mpl canvas
             * mpl navigation toolbar
             * Control panel for interaction
        """
        self.panel = wx.Panel(self)

        # Create the mpl Figure and FigCanvas objects.
        # 5x4 inches, 100 dots-per-inch
        #
        self.dpi = 100
        self.fig = figure.Figure((5.0, 4.0), dpi=self.dpi)
        self.canvas = wxagg.FigureCanvasWxAgg(self.panel, -1, self.fig)
        self.axes = self.fig.add_subplot(111)

        # Create the navigation toolbar, tied to the canvas
        #
        self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)

        #
        # Layout with box sizers
        #
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        #self.vbox.AddSpacer(25)
        self.vbox.Add(self.toolbar, 0, wx.EXPAND)

        self.panel.SetSizer(self.vbox)
        self.vbox.Fit(self)

    def draw_figure(self):
        """ Redraws the figure
        """
        # clear the axes and redraw the plot anew
        #
        self.axes.clear()
        x, y = np.random.random((10, 2)).T
        self.axes.scatter(x, y)

        self.canvas.draw()

    def on_exit(self, event):
        self.Destroy()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    app.frame = MyFrame()
    app.frame.Show()
    app.MainLoop()

这是我目前的情况:

import os
import wx
import numpy as np
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.figure as figure
import matplotlib.backends.backend_wxagg as wxagg

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(1000,700),style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)

            self.create_main_panel()
            self.draw_figure()

        def create_main_panel(self):

            self.hbox = wx.BoxSizer(wx.HORIZONTAL)

            self.left = wx.BoxSizer(wx.VERTICAL)
            self.right = wx.BoxSizer(wx.VERTICAL)

            self.pnl1 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
            self.pnl2 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
            self.pnl3 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
            self.pnl4 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)


            self.fig = figure.Figure((5.0, 4.0), dpi=100)
            self.canvas = wxagg.FigureCanvasWxAgg(self.pnl4, -1, self.fig)
            self.axes = self.fig.add_subplot(111)

            self.toolbar = wxagg.NavigationToolbar2WxAgg(self.canvas)

            self.right.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            #self.right.AddSpacer(25)
            self.right.Add(self.toolbar, 0, wx.EXPAND)

            self.left.Add(self.pnl1, 1, wx.EXPAND | wx.ALL, 3)
            self.left.Add(self.pnl2, 1, wx.EXPAND | wx.ALL, 3)
            self.right.Add(self.pnl3, 1, wx.EXPAND | wx.ALL, 3)
            #right.Add(pnl4, 1, wx.EXPAND | wx.ALL, 3)

            self.hbox.Add(self.left, 1, wx.EXPAND)
            self.hbox.Add(self.right, 1, wx.EXPAND)

            #self.SetSize((400, 120))
            self.SetSizer(self.hbox)
            self.Centre()

        def draw_figure(self):

            self.axes.clear()
            x, y = np.random.random((10, 2)).T
            self.axes.scatter(x, y)
            self.canvas.draw()

        def on_exit(self, event):
            self.Destroy()

    class MyApp(wx.App):
        def OnInit(self):
            frame = MyFrame(None, -1, 'borders.py')
            frame.Show(True)
            return True

    app = MyApp(0)
    app.MainLoop()

我试过从其他示例中提取代码。我已经尝试将图形更深地嵌入到 sizer、对话框、框架中。似乎无论我做什么,如果它是 wxPython 唯一尝试做的事情,我只能得到一个 matplotlib 无花果来绘制。 我根本无法让 wxpython 和 matplotlib 做多个框或面板。

我没有足够的信誉点数来放入图片,但第一个代码的结果是:http://i.stack.imgur.com/erhUL.png 第二个给出:http://i.stack.imgur.com/6ozk2.png

首先:在处理您示例中的 sizing/layouting 问题时,包括 wx.lib.inspection 模块。

在主循环之前的行中插入:

import wx.lib.inspection as wxli
wxli.InspectionTool().Show()

运行 你的榜样。当您单击左侧的树结构 ("Widget tree") 时,面板 1 - 3 看起来很合理。但是您会注意到,canvas 和工具栏不包含在任何 box sizer 中,这会带来麻烦。

    # add a sizer for panel 4
    pnl4_sz = wx.BoxSizer(wx.VERTICAL)
    # …
    # add your mpl stuff
    pnl4_sz.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
    assert wx.EXPAND == wx.GROW # small joke
    # …
    pnl4_sz.Add(self.toolbar, 0, wx.EXPAND)
    self.pnl4.SetSizer(pnl4_sz)

    # …
    # re-instate addition of pnl4
    self.right.Add(self.pnl4, 1, wx.EXPAND | wx.ALL, 3)