wxpython layout with sizers

有些话、适合烂在心里 提交于 2019-12-04 12:02:48

Is this what you're after?

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!