Wxpython layout with sizers

I find it hard to get my sizers working correctly in wxpython. I'm trying to make a simple horizontal bar on top (with text in it) and two vertical blocks below (with gridisers * there should be only 2 columns on the left! * Inside each). I want everything in the picture to stretch and adapt to my panel (with the ability to add additions to the sides and top / bottom). enter image description here

I have two main problems: 1. I can’t get the text in a horizontal strip to be in the middle (it goes to the left) 2. I would like to place two vertical rectangles for coverage and fit the page accordingly (I would also like the grids also better covered).

Here is my code (with missing parts):

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) 
+4
source share
3 answers

This is what you need?

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() 
+3
source

something like that??

 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() 
+1
source

Here is one way to do this:

 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 watching a wxPython demo. I think it can only be supported in wx.GridBagSizer and FlexGridSizer. However, you can try the span parameter. In addition, it should be noted that wx.GROW and wx.EXPAND are the same. You can also check the wiki for more information: http://wiki.wxpython.org/GridBagSizerTutorial

+1
source

Source: https://habr.com/ru/post/1494579/


All Articles