Search code examples
pythonlayoutwxpythonsizer

wxpython layout with sizers


I'm having difficulty getting my sizers to work properly in wxpython. I am trying to do a simple one horizontal bar at top (with text in it) and two vertical boxes below (with gridsizers * the left one should only be 2 columns!! * inside each). I want the everything in the image to stretch and fit my panel as well (with the ability to add padding to sides and top/bottom). enter image description here

I have two main issues: 1. I cant get the text in the horizontal bar to be in the middle (it goes to the left) 2. I would like to space the two vertical boxes to span AND fit the page appropriately (also would like the grids to span better too).

Here is my code (with some parts omitted):

            self.LeagueInfoU = wx.Panel(self.LeagueInfo,-1, style=wx.BORDER_NONE)
            self.LeagueInfoL = wx.Panel(self.LeagueInfo,-1, style=wx.BORDER_NONE)
            self.LeagueInfoR = wx.Panel(self.LeagueInfo,-1, style=wx.BORDER_NONE)

            vbox  = wx.BoxSizer(wx.VERTICAL)

            hbox1 = wx.BoxSizer(wx.HORIZONTAL)
            hbox2 = wx.BoxSizer(wx.HORIZONTAL)

            vbox2a = wx.GridSizer(12,2,0,0)
            vbox3a = wx.GridSizer(10,3,0,0)
            hbox1a = wx.BoxSizer(wx.VERTICAL)

            vbox2 = wx.BoxSizer(wx.VERTICAL)
            vbox3 = wx.BoxSizer(wx.VERTICAL)


            hbox1.Add(self.LeagueInfoU, 1, wx.EXPAND | wx.ALL, 3)
            vbox2.Add(self.LeagueInfoL, 1, wx.EXPAND | wx.ALL, 3)
            vbox3.Add(self.LeagueInfoR, 1, wx.EXPAND | wx.ALL, 3)

            vbox2a.AddMany([this is all correct])
            self.LeagueInfoL.SetSizer(vbox2a)

            vbox3a.AddMany([this is all correct])  
            self.LeagueInfoR.SetSizer(vbox3a)

            font = wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.BOLD)
            self.Big_Header = wx.StaticText(self.LeagueInfoU, -1, 'Testing This')
            self.Big_Header.SetFont(font)

            hbox1a.Add(self.Big_Header, 0, wx.ALIGN_CENTER|wx.ALIGN_CENTER_VERTICAL)        
            self.LeagueInfoU.SetSizer(hbox1a)

            hbox2.Add(vbox2, 0, wx.EXPAND)
            hbox2.Add(vbox3, 0, wx.EXPAND)

            vbox.Add(hbox1, 0, wx.EXPAND)
            vbox.Add(hbox2, 1, wx.EXPAND)
            self.LeagueInfo.SetSizer(vbox)

Solution

  • Is this what you're after?

    enter image description here

    import wx
    
    class Frame(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent)
    
            self.panel = wx.Panel(self)
    
            main_sizer = wx.BoxSizer(wx.VERTICAL)
    
            # Title
            self.centred_text = wx.StaticText(self.panel, label="Title")
            main_sizer.Add(self.centred_text, 0, wx.ALIGN_CENTRE | wx.ALL, 3)
    
            # Grids
            content_sizer = wx.BoxSizer(wx.HORIZONTAL)
            grid_1 = wx.GridSizer(12, 2, 0, 0)
            grid_1.AddMany(wx.StaticText(self.panel, label=str(i)) for i in xrange(24))
            content_sizer.Add(grid_1, 1, wx.EXPAND | wx.ALL, 3)
            grid_2 = wx.GridSizer(10, 3, 0, 0)
            grid_2.AddMany(wx.StaticText(self.panel, label=str(i)) for i in xrange(30))
            content_sizer.Add(grid_2, 1, wx.EXPAND | wx.ALL, 3)
    
            main_sizer.Add(content_sizer, 1, wx.EXPAND)
    
            self.panel.SetSizer(main_sizer)
    
            self.Show()
    
    
    if __name__ == "__main__":
    
        app = wx.App(False)
        Frame(None)
        app.MainLoop()