How to create a grid sizer (Phoenix)

Keywords : Grid sizer.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

wx.GridSizer lays out its children in a two-dimensional table.

The width of each field is the width of the widest child.

The height of each field is the height of the tallest child.

wx.GridSizer(integer rows, integer cols, integer vgap, integer hgap)

In the constructor we provide the number of rows and the number of columns of our table and the horizontal and vertical gap between the children widgets.

We insert our widgets into the table with the AddMany() method.

Children are inserted from left to right, top to bottom.

The formula we input is processed by the eval built-in python function.

output = eval(formula)

If we make an error in our formula, an error message is displayed.

Notice how we managed to put a space between the Bck and Close buttons.

We simply put an empty wx.StaticText there. Such tricks are quite common.

(info by ZetCode / Jan Bodnar).

   1 # sample_one.py
   2 
   3 """
   4 
   5 Author : Jan Bodnar
   6 Website : zetcode.com
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyFrame
  13 # class MyApp
  14 
  15 #---------------------------------------------------------------------------
  16 
  17 class MyFrame(wx.Frame):
  18     def __init__(self, parent, id, title):
  19         wx.Frame.__init__(self, parent, id, title,
  20                           wx.DefaultPosition, wx.Size(300, 250))
  21 
  22         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  23         self.SetBackgroundColour("#eceade")
  24         self.SetMinSize((300, 250))
  25 
  26         #------------
  27 
  28         self.formula = False
  29 
  30         #------------
  31 
  32         menubar = wx.MenuBar()
  33 
  34         file = wx.Menu()
  35         file.Append(22, '&Quit', 'Exit Calculator')
  36 
  37         menubar.Append(file, '&File')
  38 
  39         self.SetMenuBar(menubar)
  40 
  41         #------------
  42 
  43         self.display = wx.TextCtrl(self, -1, '',  style=wx.TE_RIGHT)
  44 
  45         #------------
  46 
  47         sizer = wx.BoxSizer(wx.VERTICAL)
  48         sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)
  49 
  50         #------------
  51 
  52         gs = wx.GridSizer(5, 4, 3, 3)
  53 
  54         gs.AddMany([(wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
  55                     (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
  56                     (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
  57                     (wx.Button(self, 22, '&Close'), 0, wx.EXPAND),
  58                     (wx.Button(self, 1, '7'), 0, wx.EXPAND),
  59                     (wx.Button(self, 2, '8'), 0, wx.EXPAND),
  60                     (wx.Button(self, 3, '9'), 0, wx.EXPAND),
  61                     (wx.Button(self, 4, '/'), 0, wx.EXPAND),
  62                     (wx.Button(self, 5, '4'), 0, wx.EXPAND),
  63                     (wx.Button(self, 6, '5'), 0, wx.EXPAND),
  64                     (wx.Button(self, 7, '6'), 0, wx.EXPAND),
  65                     (wx.Button(self, 8, '*'), 0, wx.EXPAND),
  66                     (wx.Button(self, 9, '1'), 0, wx.EXPAND),
  67                     (wx.Button(self, 10, '2'), 0, wx.EXPAND),
  68                     (wx.Button(self, 11, '3'), 0, wx.EXPAND),
  69                     (wx.Button(self, 12, '-'), 0, wx.EXPAND),
  70                     (wx.Button(self, 13, '0'), 0, wx.EXPAND),
  71                     (wx.Button(self, 14, '.'), 0, wx.EXPAND),
  72                     (wx.Button(self, 15, '='), 0, wx.EXPAND),
  73                     (wx.Button(self, 16, '+'), 0, wx.EXPAND)])
  74 
  75         sizer.Add(gs, 1, wx.EXPAND)
  76 
  77         self.SetSizer(sizer)
  78 
  79         #------------
  80 
  81         self.Bind(wx.EVT_BUTTON, self.OnClear, id=20)
  82         self.Bind(wx.EVT_BUTTON, self.OnBackspace, id=21)
  83         self.Bind(wx.EVT_BUTTON, self.OnClose, id=22)
  84         self.Bind(wx.EVT_BUTTON, self.OnSeven, id=1)
  85         self.Bind(wx.EVT_BUTTON, self.OnEight, id=2)
  86         self.Bind(wx.EVT_BUTTON, self.OnNine, id=3)
  87         self.Bind(wx.EVT_BUTTON, self.OnDivide, id=4)
  88         self.Bind(wx.EVT_BUTTON, self.OnFour, id=5)
  89         self.Bind(wx.EVT_BUTTON, self.OnFive, id=6)
  90         self.Bind(wx.EVT_BUTTON, self.OnSix, id=7)
  91         self.Bind(wx.EVT_BUTTON, self.OnMultiply, id=8)
  92         self.Bind(wx.EVT_BUTTON, self.OnOne, id=9)
  93         self.Bind(wx.EVT_BUTTON, self.OnTwo, id=10)
  94         self.Bind(wx.EVT_BUTTON, self.OnThree, id=11)
  95         self.Bind(wx.EVT_BUTTON, self.OnMinus, id=12)
  96         self.Bind(wx.EVT_BUTTON, self.OnZero, id=13)
  97         self.Bind(wx.EVT_BUTTON, self.OnDot, id=14)
  98         self.Bind(wx.EVT_BUTTON, self.OnEqual, id=15)
  99         self.Bind(wx.EVT_BUTTON, self.OnPlus, id=16)
 100         self.Bind(wx.EVT_MENU, self.OnClose, id=22)
 101 
 102         #------------
 103 
 104         self.Centre()
 105 
 106     #-----------------------------------------------------------------------
 107 
 108     def OnClear(self, event):
 109         self.display.Clear()
 110 
 111 
 112     def OnBackspace(self, event):
 113         formula = self.display.GetValue()
 114         self.display.Clear()
 115         self.display.SetValue(formula[:-1])
 116 
 117 
 118     def OnClose(self, event):
 119         self.Close()
 120 
 121 
 122     def OnDivide(self, event):
 123         if self.formula:
 124             return
 125         self.display.AppendText('/')
 126 
 127 
 128     def OnMultiply(self, event):
 129         if self.formula:
 130             return
 131         self.display.AppendText('*')
 132 
 133 
 134     def OnMinus(self, event):
 135         if self.formula:
 136             return
 137         self.display.AppendText('-')
 138 
 139 
 140     def OnPlus(self, event):
 141         if self.formula:
 142             return
 143         self.display.AppendText('+')
 144 
 145 
 146     def OnDot(self, event):
 147         if self.formula:
 148             return
 149         self.display.AppendText('.')
 150 
 151 
 152     def OnEqual(self, event):
 153         if self.formula:
 154             return
 155         formula = self.display.GetValue()
 156         self.formula = False
 157         try:
 158             self.display.Clear()
 159             output = eval(formula)
 160             self.display.AppendText(str(output))
 161         except StandardError:
 162             self.display.AppendText("Error")
 163 
 164 
 165     def OnZero(self, event):
 166         if self.formula:
 167             self.display.Clear()
 168             self.formula = False
 169         self.display.AppendText('0')
 170 
 171 
 172     def OnOne(self, event):
 173         if self.formula:
 174             self.display.Clear()
 175             self.formula = False
 176         self.display.AppendText('1')
 177 
 178 
 179     def OnTwo(self, event):
 180         if self.formula:
 181             self.display.Clear()
 182             self.formula = False
 183         self.display.AppendText('2')
 184 
 185 
 186     def OnThree(self, event):
 187         if self.formula:
 188             self.display.Clear()
 189             self.formula = False
 190         self.display.AppendText('3')
 191 
 192 
 193     def OnFour(self, event):
 194         if self.formula:
 195             self.display.Clear()
 196             self.formula = False
 197         self.display.AppendText('4')
 198 
 199 
 200     def OnFive(self, event):
 201         if self.formula:
 202             self.display.Clear()
 203             self.formula = False
 204         self.display.AppendText('5')
 205 
 206 
 207     def OnSix(self, event):
 208         if self.formula:
 209             self.display.Clear()
 210             self.formula = False
 211         self.display.AppendText('6')
 212 
 213 
 214     def OnSeven(self, event):
 215         if self.formula:
 216             self.display.Clear()
 217             self.formula = False
 218         self.display.AppendText('7')
 219 
 220 
 221     def OnEight(self, event):
 222         if self.formula:
 223             self.display.Clear()
 224             self.formula = False
 225         self.display.AppendText('8')
 226 
 227 
 228     def OnNine(self, event):
 229         if self.formula:
 230             self.display.Clear()
 231             self.formula = False
 232         self.display.AppendText('9')
 233 
 234 #---------------------------------------------------------------------------
 235 
 236 class MyApp(wx.App):
 237     def OnInit(self):
 238         frame = MyFrame(None, -1, 'Calculator')
 239         frame.Show(True)
 240         self.SetTopWindow(frame)
 241 
 242         return True
 243 
 244 #---------------------------------------------------------------------------
 245 
 246 app = MyApp(0)
 247 app.MainLoop()


Sample two

img_sample_two.png

   1 # sample_two.py
   2 
   3 """
   4 
   5 https://maku77.github.io/python/wxpython/layout.html
   6 
   7 """
   8 
   9 import wx
  10 
  11 # class MyFrame
  12 
  13 #---------------------------------------------------------------------------
  14 
  15 class MyFrame(wx.Frame):
  16     def __init__(self):
  17         wx.Frame.__init__(self, None, -1,
  18                           "wx.GridSizer", size=(300, 150))
  19 
  20         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  21 
  22         #------------
  23 
  24         self.InitializeComponents()
  25 
  26     #-----------------------------------------------------------------------
  27 
  28     def InitializeComponents(self):
  29         mainPanel = wx.Panel(self)
  30 
  31         button1 = wx.Button(mainPanel, -1, "Button 1")
  32         button2 = wx.Button(mainPanel, -1, "Button 2")
  33         button3 = wx.Button(mainPanel, -1, "Button 3")
  34         button4 = wx.Button(mainPanel, -1, "Button 4")
  35         button5 = wx.Button(mainPanel, -1, "Button 5")
  36         button6 = wx.Button(mainPanel, -1, "Button 6")
  37 
  38         #------------
  39 
  40         # Create a sizer.
  41         sizer = wx.GridSizer(2, 3, 1)
  42 
  43         sizer.Add(button1)
  44         sizer.Add(button2)
  45         sizer.Add(button3)
  46         sizer.Add(button4)
  47         sizer.Add(button5)
  48         sizer.Add(button6)
  49 
  50         mainPanel.SetSizer(sizer)
  51 
  52 #---------------------------------------------------------------------------
  53 
  54 if __name__ == '__main__':
  55     app = wx.App()
  56     MyFrame().Show(True)
  57     app.MainLoop()


Sample three

img_sample_three.png

   1 # sample_three.py
   2 
   3 """
   4 
   5 https://maku77.github.io/python/wxpython/layout.html
   6 
   7 """
   8 
   9 import wx
  10 
  11 # class MyFrame
  12 
  13 #---------------------------------------------------------------------------
  14 
  15 class MyFrame(wx.Frame):
  16     def __init__(self):
  17         wx.Frame.__init__(self, None, -1,
  18                           "wx.GridSizer (vgap + hgap)", size=(360, 150))
  19 
  20         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  21 
  22         #------------
  23 
  24         self.InitializeComponents()
  25 
  26     #-----------------------------------------------------------------------
  27 
  28     def InitializeComponents(self):
  29         mainPanel = wx.Panel(self)
  30 
  31         button1 = wx.Button(mainPanel, -1, "Button 1")
  32         button2 = wx.Button(mainPanel, -1, "Button 2")
  33         button3 = wx.Button(mainPanel, -1, "Button 3")
  34         button4 = wx.Button(mainPanel, -1, "Button 4")
  35         button5 = wx.Button(mainPanel, -1, "Button 5")
  36         button6 = wx.Button(mainPanel, -1, "Button 6")
  37 
  38         #------------
  39 
  40         # Create a sizer.
  41         sizer = wx.GridSizer(2, 3, 5, 20)
  42 
  43         sizer.Add(button1, flag=wx.EXPAND)
  44         sizer.Add(button2, flag=wx.EXPAND)
  45         sizer.Add(button3, flag=wx.EXPAND)
  46         sizer.Add(button4, flag=wx.EXPAND)
  47         sizer.Add(button5, flag=wx.EXPAND)
  48         sizer.Add(button6, flag=wx.EXPAND)
  49 
  50         mainPanel.SetSizer(sizer)
  51 
  52 #---------------------------------------------------------------------------
  53 
  54 if __name__ == '__main__':
  55     app = wx.App()
  56     MyFrame().Show(True)
  57     app.MainLoop()


Sample four

img_sample_four.png

   1 # sample_four.py
   2 
   3 """
   4 
   5 https://maku77.github.io/python/wxpython/layout.html
   6 
   7 """
   8 
   9 import wx
  10 
  11 # class MyFrame
  12 
  13 #---------------------------------------------------------------------------
  14 
  15 class MyFrame(wx.Frame):
  16     def __init__(self):
  17         wx.Frame.__init__(self, None, -1,
  18                           "wx.GridSizer", size=(300, 150))
  19 
  20         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  21 
  22         #------------
  23 
  24         self.InitializeComponents()
  25 
  26     #-----------------------------------------------------------------------
  27 
  28     def InitializeComponents(self):
  29         mainPanel = wx.Panel(self)
  30 
  31         button1 = wx.Button(mainPanel, -1, "Button 1")
  32         button2 = wx.Button(mainPanel, -1, "Button 2")
  33         button3 = wx.Button(mainPanel, -1, "Button 3")
  34         button4 = wx.Button(mainPanel, -1, "Button 4")
  35         button5 = wx.Button(mainPanel, -1, "Button 5")
  36         button6 = wx.Button(mainPanel, -1, "Button 6")
  37 
  38         #------------
  39 
  40         # Create a sizer.
  41         sizer = wx.GridSizer(2, 3, (0, 0))
  42 
  43         sizer.Add(button1, flag=wx.EXPAND)
  44         sizer.Add(button2, flag=wx.ALIGN_CENTER)
  45         sizer.Add(button3, flag=wx.EXPAND)
  46         sizer.Add(button4, flag=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
  47         sizer.Add(button5, flag=wx.EXPAND)
  48         sizer.Add(button6, flag=wx.ALIGN_LEFT | wx.ALIGN_BOTTOM)
  49 
  50         mainPanel.SetSizer(sizer)
  51 
  52 #---------------------------------------------------------------------------
  53 
  54 if __name__ == '__main__':
  55     app = wx.App()
  56     MyFrame().Show(True)
  57     app.MainLoop()


Download source

source.zip


Additional Information

Link :


https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Maku77 (sample_one / two / three / four.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

07/01/21 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah...

How to create a grid sizer (Phoenix) (last edited 2021-01-14 17:00:44 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.