Lingfeng Xiong
Lingfeng Xiong

Reputation: 1171

wxWidgets: Panel inside BoxSizer doesn't behaviours as expected

I use Panel to draw images from OpenCV, but it seems this panel cannot be placed into BoxSizer. I tried to do

vbox = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox)
vbox.Add(wx.Button(panel, label='aaa', size=(70, 30)))
vbox.Add(wx.Button(panel, label='aaa', size=(70, 30)))
vbox.Add(wx.Button(panel, label='aaa', size=(70, 30)))
vbox.Add(OpenCVCanvas(self), border=10)

The buttons layout correctly, but the panel is always placed at the left top corner. Is there anything I did wrong?

Code for panel:

class OpenCVCanvas(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.Bind(wx.EVT_PAINT, self.onPaint)
        self.bmp = None

    def updateImage(self, frame):
        if self.bmp is None:
            height, width = frame.shape[:2]
            self.SetSize((width, height))
            self.bmp = wx.BitmapFromBuffer(width, height, frame)
        self.bmp.CopyFromBuffer(frame)
        self.Refresh()

    def onPaint(self, evt):
        if self.bmp is not None:
            dc = wx.BufferedPaintDC(self)
            dc.DrawBitmap(self.bmp, 0, 0)

Upvotes: 2

Views: 427

Answers (1)

RobinDunn
RobinDunn

Reputation: 6306

This is likely the source of the problem:

vbox.Add(OpenCVCanvas(self), border=10)

You are creating buttons that are children of panel and addign them to the sizer which is set to the the panel's sizer, but the OpenCVCanvas is using self as its parent which I assume is the parent of panel which means that the OpenCVCanvas is a sibling of panel, not its child. The sizer can not manage the size and layout of an item that is not a child of the window the sizer is assigned to.

Upvotes: 2

Related Questions