带有多个面板

时间:2015-07-08 07:25:19

标签: matplotlib wxpython

我试图将此示例转换为多面板示例。

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,对话框和框架中。似乎无论我做什么,我只能得到一个matplotlib图,如果它是wxPython试图做的唯一的东西。 我根本不能让wxpython和matplotlib做多个盒子或面板。

我没有足够的声望点来放置图片,但第一个代码会产生:http://i.stack.imgur.com/erhUL.png 第二个给出:http://i.stack.imgur.com/6ozk2.png

1 个答案:

答案 0 :(得分:0)

首先:在处理示例中的大小调整/布局问题时,请包含wx.lib.inspection模块。

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

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

运行你的例子。当您单击左侧的树结构(“窗口小部件树”)时,面板1-3看起来合理。但是你会注意到,任何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)