Question

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)
Was it helpful?

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()

OTHER TIPS

something like this??

import wx



class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Test Stretching!!")
        p1 = wx.Panel(self,-1,size=(500,100))
        p1.SetMinSize((500,100))
        p1.SetBackgroundColour(wx.GREEN)

        hsz = wx.BoxSizer(wx.HORIZONTAL)

        p2 = wx.Panel(self,-1,size=(200,400))
        p2.SetMinSize((200,400))
        p2.SetBackgroundColour(wx.RED)

        p3 = wx.Panel(self,-1,size=(300,400))
        p3.SetMinSize((300,400))
        p3.SetBackgroundColour(wx.BLUE)

        hsz.Add(p2,1,wx.EXPAND)
        hsz.Add(p3,1,wx.EXPAND)
        sz = wx.BoxSizer(wx.VERTICAL)
        sz.Add(p1,0,wx.EXPAND)
        sz.Add(hsz,1,wx.EXPAND)
        self.SetSizer(sz)
        self.Layout()
        self.Fit()

a = wx.App(redirect=False)
f = MyFrame()
f.Show()
a.MainLoop()

Here's one way to do it:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        leftGridSizer = wx.GridSizer(rows=10, cols=12, vgap=5, hgap=5)
        rightGridSizer = wx.GridSizer(rows=10, cols=3, vgap=5, hgap=5)


        title = wx.StaticText(self, label="Main title")

        mainSizer.Add(wx.StaticText(self), 0, wx.EXPAND)  # add a "spacer"
        mainSizer.Add(title, 0, wx.CENTER, wx.ALL, 10)

        for row in range(1, 11):
            for col in range(1, 13):
                lbl = "Row%s Col%s" % (row, col)
                leftGridSizer.Add(wx.StaticText(self, label=lbl))
        hSizer.Add(leftGridSizer, 0, wx.ALL, 20)

        for row in range(1, 11):
            for col in range(1, 4):
                lbl = "Row%s Col%s" % (row, col)
                rightGridSizer.Add(wx.StaticText(self, label=lbl))

        hSizer.Add(rightGridSizer, 0, wx.ALL, 20)
        mainSizer.Add(hSizer)

        self.SetSizer(mainSizer)

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Sizers", size=(1600,600))
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

To learn about spanning rows, I recommend looking at the wxPython demo. I think that may only be supported in wx.GridBagSizer and the FlexGridSizer though. You can try the span parameter though. Also, it should be noted that wx.GROW and wx.EXPAND are one and the same. You might also want to check out the wiki for more information: http://wiki.wxpython.org/GridBagSizerTutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top