Why are two widgets on top of each other?

Hello,

Because of the sizer’s wx.HORIZONTAL, I expected the two widgets to sit next to each other, but they’re on top:

image

Any idea why?

Thank you.

import wx

class MainWindow(wx.Frame):
  def __init__(self, parent, title):
    wx.Frame.__init__(self, parent, title=title, size=(200,100))

    #is self. needed if property/object not referenced elsewhere in class?
    self.panel =  wx.Panel(self, wx.ID_ANY)
    self.panel.SetFocus()

    lines=['item1','item2']
    self.lstbx= wx.ListBox(self.panel, -1, choices=['item1','item2'], style=(wx.LB_EXTENDED | wx.LB_ALWAYS_SB))

    txtctrl = wx.TextCtrl(self.panel, value="", style=wx.TE_MULTILINE)

    #why are two widgets on top of each other instead of side by side?
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    sizer.Add(self.lstbx,flag=wx.GROW)
    sizer.Add(txtctrl,flag=wx.GROW)

if __name__ == "__main__":
  app = wx.App(False)
  gui = MainWindow(None, "test")
  gui.Show()
  app.MainLoop()

You need to set the sizer to the control that is the parent of the controls you have added to the sizer. In your case that would be self.panel.

Add the following line to the end of __init__():

    self.panel.SetSizer(sizer)
1 Like

Makes sense. Thank you.