0
votes

I am trying to turn this example into a multi-panel example.

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

Here is what I have so far:

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

I have tried pulling in code from other examples. I've tried deeper embedding of the figure into sizers, dialogs, frames. It seems no matter what I do I can only get a matplotlib fig to draw if its the only thing wxPython is trying to do. I cannot get wxpython and matplotlib to do multiple boxes or panels at all.

I don't have enough reputation points to put the pictures in but the first code results in : http://i.stack.imgur.com/erhUL.png The second gives : http://i.stack.imgur.com/6ozk2.png

1

1 Answers

0
votes

First: When dealing with an sizing/layouting issue like in your example, include the wx.lib.inspection module.

In the line before the main loop insert:

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

Run your example. When you click through the tree structure left ("Widget tree"), panel 1 - 3 look reasonable. But you will notice, that canvas and toolbar is not included into any box sizer, and this is giving trouble.

    # 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)