Search code examples
pythonuser-interfacewxpythonwxwidgets

wxPython using a sizer with panels


I'm trying to make a GUI with on the left side a camera input with some data on the camera stream. On the right side I want some buttons and other widgets. The code a have so far: (the functions get_image() and pil_to_wx() work fine, they're just not shown in the code below)

class HUDPanel(wx.Panel):
    def __init__(self, parent):
        super(HUDPanel, self).__init__(parent, -1)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.Bind(wx.EVT_PAINT, self.on_paint)
        self.update()

    def update(self):
        self.Refresh()
        self.Update()
        wx.CallLater(15, self.update)

    def create_bitmap(self):
        image = get_image()
        bitmap = pil_to_wx(image)
        return bitmap

    def on_paint(self, event):
        bitmap = self.create_bitmap()
        dc = wx.AutoBufferedPaintDC(self)
        dc.DrawBitmap(bitmap, 0, 0)


class ExtraPanel(wx.Panel):
    def __init__(self, parent):
        super(ExtraPanel, self).__init__(parent, -1)
        My_Button = wx.Button(self,label="TEST")


class Frame(wx.Frame):
    def __init__(self):
        style = wx.DEFAULT_FRAME_STYLE & ~wx.RESIZE_BORDER & ~wx.MAXIMIZE_BOX
        super(Frame, self).__init__(None, -1, 'Camera Viewer', style=style)

        my_sizer = wx.BoxSizer(wx.HORIZONTAL)

        campanel = HUDPanel(self)
        my_sizer.Add(campanel, 0, wx.ALL | wx.CENTER, 5)

        widgetpanel = ExtraPanel(self)
        my_sizer.Add(widgetpanel, 0, wx.ALL | wx.CENTER, 5)

        self.SetSizer(my_sizer)


        self.Fit()


def main():
    app = wx.App()
    frame = Frame()

    frame.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()

When I run this code, all I get is a small window with only a button named "TEST" (the panel that should be on the right side). The previous version with only the camera panel worked fine, so that's not the problem. What am I doing wrong?

UPDATE: The sizes of the sub-panels are fixed, i see the panel with the camera show up but only a small line on the screen. The part with the button show perfectly.


Solution

  • First of all, you don't give any size to your HUDPanel, so I'm not sure how do you expect it to appear.

    Second, you're recursively calling update all the time (well every 15ms), which is most definitely a bad idea as this will consume close to 100% of (one) CPU and may prevent your application from dispatching other events.