Search code examples
python-2.7wxpython

Unable to display panel at the desired position


I created a Panel in a wxFrame and then created a FigureCanvas. I would like to fit the FigureCanvas into the panel completely, but somehow the FigureCanvas does not go into the panel2_2, but exactly on just the opposite side.

Following is my code.

import wx
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure


class Frame1(wx.Frame):

    def __init__(self, prnt):

        wx.Frame.__init__(self, parent=prnt,
                          pos=wx.Point(0, 0), size=wx.Size(1340, 720),
                          style=wx.DEFAULT_FRAME_STYLE)

        self.panel2_2 = wx.Panel(parent=self,
                                 pos=wx.Point(940, 30), size=wx.Size(400, 690),
                                 style=wx.TAB_TRAVERSAL)

        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.panel2_2, -1, self.figure)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.canvas, 0, wx.EXPAND)
        self.panel2_2.SetSizer(sizer)
        self.panel2_2.Fit()

        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)


#Every wxWidgets application must have a class derived from wxApp
class MyApp(wx.App):
# wxWidgets calls this method to initialize the application
    def OnInit(self):

        # Create an instance of our customized Frame class
        self.frame = Frame1(None)
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True


if __name__ == '__main__':
    application = MyApp(0)
    application.MainLoop()

and the result

enter image description here

I would like to have the image on the panel2_2 ( that is on the right hand side ) and not on the left hand side


Solution

  • I think the canvas is indeed going to panel2_2. The problem in your MWE is that you do not define a sizer for panel2_2. Therefore, panel2_2 is rendered from the upper left corner of the frame. This results in panel2_2 plus the canvas showing in the left of the frame. What you see to the right of the canvas is not panel2_2 but the rest of the frame since the size of the frame is larger than the size of panel2_2. If you add a blue panel1_1 and assign another wx.BoxSizer to the frame you get the canvas showing to the right.

    import wx
    from numpy import arange, sin, pi
    import matplotlib
    matplotlib.use('WXAgg')
    
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
    from matplotlib.figure import Figure
    
    
    class Frame1(wx.Frame):
    
        def __init__(self, prnt):
    
            wx.Frame.__init__(self, parent=prnt,
                              pos=wx.Point(0, 0), size=wx.Size(1340, 720),
                              style=wx.DEFAULT_FRAME_STYLE)
    
            self.panel1_1 = wx.Panel(parent=self, size=(400, 690))
            self.panel1_1.SetBackgroundColour('blue')
    
            self.panel2_2 = wx.Panel(parent=self,
                                     pos=wx.Point(940, 30), size=wx.Size(400, 690),
                                     style=wx.TAB_TRAVERSAL)
    
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            self.canvas = FigureCanvas(self.panel2_2, -1, self.figure)
    
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.canvas, 0, wx.EXPAND)
            self.panel2_2.SetSizer(sizer)
            self.panel2_2.Fit()
    
            sizerPanels = wx.BoxSizer(wx.HORIZONTAL)
            sizerPanels.Add(self.panel1_1)
            sizerPanels.Add(self.panel2_2)
            sizerPanels.Fit(self)
            self.SetSizer(sizerPanels)
    
            t = arange(0.0, 3.0, 0.01)
            s = sin(2 * pi * t)
            self.axes.plot(t, s)
    
            self.Center()
    
    
    #Every wxWidgets application must have a class derived from wxApp
    class MyApp(wx.App):
    # wxWidgets calls this method to initialize the application
        def OnInit(self):
    
            # Create an instance of our customized Frame class
            self.frame = Frame1(None)
            self.SetTopWindow(self.frame)
            self.frame.Show()
            return True
    
    
    if __name__ == '__main__':
        application = MyApp(0)
        application.MainLoop()