.. include:: headings.inc .. _GridBagSizer: ========================================================================================================================================== |phoenix_title| **GridBagSizer** ========================================================================================================================================== A :ref:`Sizer` that can lay out items in a virtual grid like a :ref:`FlexGridSizer` but in this case explicit positioning of the items is allowed using :ref:`GBPosition`, and items can optionally span more than one row and/or column using :ref:`GBSpan`. | |class_hierarchy| Inheritance Diagram ===================================== Inheritance diagram for class **GridBagSizer** .. raw:: html

Inheritance diagram of GridBagSizer

| |user| Contributed Examples =========================== .. raw:: html
Example 1 - Chris Barker (:download:`download <_downloads/GridBagSizer.1.py>`):: #!/usr/bin/env python """ A simple test of the GridBagSizer http://wiki.wxpython.org/index.cgi/WriteItYourself """ import wx class MyFrame(wx.Frame): def __init__(self, parent, ID, title): wx.Frame.__init__(self, parent, ID, title, wx.DefaultPosition) Buttons = [] for i in range(6): Buttons.append(wx.Button(self,-1, "Button %i"%(i))) sizer = wx.GridBagSizer(9, 9) sizer.Add(Buttons[0], (0, 0), wx.DefaultSpan, wx.ALL, 5) sizer.Add(Buttons[1], (1, 1), (1,7), wx.EXPAND) sizer.Add(Buttons[2], (6, 6), (3,3), wx.EXPAND) sizer.Add(Buttons[3], (3, 0), (1,1), wx.ALIGN_CENTER) sizer.Add(Buttons[4], (4, 0), (1,1), wx.ALIGN_LEFT) sizer.Add(Buttons[5], (5, 0), (1,1), wx.ALIGN_RIGHT) sizer.AddGrowableRow(6) sizer.AddGrowableCol(6) self.SetSizerAndFit(sizer) self.Centre() class MyApp(wx.App): def OnInit(self): frame = MyFrame(None, -1, "wx.gridbagsizer.py") frame.Show(True) self.SetTopWindow(frame) return True if __name__ == "__main__": app = MyApp(0) app.MainLoop() .. raw:: html
.. raw:: html
Example 2 - Chris Barker (:download:`download <_downloads/GridBagSizer.2.py>`):: #!/usr/bin/env python import wx class TestFrame(wx.Frame): def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **kwargs) t = wx.TextCtrl(self) b1 = wx.Button(self, label="Button1") b2 = wx.Button(self, label="Button2") exitBut = wx.Button(self, label="Exit") exitBut.Bind(wx.EVT_BUTTON, self.OnCloseWindow) sizer = wx.GridBagSizer(10, 10) sizer.Add(t, (0,0), span=(2,1), flag=wx.ALIGN_CENTER_VERTICAL ) sizer.Add(b1, (0,1), span=(2,1), flag=wx.ALIGN_CENTER) sizer.Add(b2, (0,2), flag=wx.ALIGN_CENTER) sizer.Add(exitBut, (1,3)) self.SetSizerAndFit(sizer) wx.EVT_CLOSE(self, self.OnCloseWindow) def OnCloseWindow(self, event): self.Destroy() class App(wx.App): def OnInit(self): frame = TestFrame(None, title="GridBagSizer Test") self.SetTopWindow(frame) frame.Show(True) return True if __name__ == "__main__": app = App(0) app.MainLoop() .. raw:: html
|method_summary| Methods Summary ================================ ================================================================================ ================================================================================ :meth:`~GridBagSizer.__init__` Constructor, with optional parameters to specify the gap between the rows and columns. :meth:`~GridBagSizer.Add` Adds the given item to the given position. :meth:`~GridBagSizer.CalcMin` Called when the managed size of the sizer is needed or when layout needs done. :meth:`~GridBagSizer.CheckForIntersection` Look at all items and see if any intersect (or would overlap) the given item. :meth:`~GridBagSizer.FindItem` Find the sizer item for the given window or subsizer, returns ``None`` if not found. :meth:`~GridBagSizer.FindItemAtPoint` Return the sizer item located at the point given in pt, or ``None`` if there is no item at that point. :meth:`~GridBagSizer.FindItemAtPosition` Return the sizer item for the given grid cell, or ``None`` if there is no item at that position. :meth:`~GridBagSizer.FindItemWithData` Return the sizer item that has a matching user data (it only compares pointer values) or ``None`` if not found. :meth:`~GridBagSizer.GetCellSize` Get the size of the specified cell, including hgap and vgap. :meth:`~GridBagSizer.GetEmptyCellSize` Get the size used for cells in the grid with no item. :meth:`~GridBagSizer.GetItemPosition` Get the grid position of the specified item. :meth:`~GridBagSizer.GetItemSpan` Get the row/col spanning of the specified item. :meth:`~GridBagSizer.RecalcSizes` Called when the managed size of the sizer is needed or when layout needs done. :meth:`~GridBagSizer.SetEmptyCellSize` Set the size used for cells in the grid with no item. :meth:`~GridBagSizer.SetItemPosition` Set the grid position of the specified item. :meth:`~GridBagSizer.SetItemSpan` Set the row/col spanning of the specified item. ================================================================================ ================================================================================ | |property_summary| Properties Summary ===================================== ================================================================================ ================================================================================ :attr:`~GridBagSizer.EmptyCellSize` See :meth:`~GridBagSizer.GetEmptyCellSize` and :meth:`~GridBagSizer.SetEmptyCellSize` ================================================================================ ================================================================================ | |api| Class API =============== .. class:: GridBagSizer(FlexGridSizer) A Sizer that can lay out items in a virtual grid like a FlexGridSizer but in this case explicit positioning of the items is allowed using GBPosition, and items can optionally span more than one row and/or column using GBSpan. **Possible constructors**:: GridBagSizer(vgap=0, hgap=0) .. method:: __init__(self, vgap=0, hgap=0) Constructor, with optional parameters to specify the gap between the rows and columns. :param `vgap`: :type `vgap`: int :param `hgap`: :type `hgap`: int .. method:: Add(self, *args, **kw) Adds the given item to the given position. :returns: A valid pointer if the item was successfully placed at the given position, or ``None`` if something was already there. |overload| **Overloaded Implementations**: **~~~** **Add** `(self, window, pos, span=DefaultSpan, flag=0, border=0, userData=None)` :param `window`: :type `window`: Window :param `pos`: :type `pos`: GBPosition :param `span`: :type `span`: GBSpan :param `flag`: :type `flag`: int :param `border`: :type `border`: int :param `userData`: :type `userData`: PyUserData :rtype: :ref:`SizerItem` **~~~** **Add** `(self, sizer, pos, span=DefaultSpan, flag=0, border=0, userData=None)` :param `sizer`: :type `sizer`: Sizer :param `pos`: :type `pos`: GBPosition :param `span`: :type `span`: GBSpan :param `flag`: :type `flag`: int :param `border`: :type `border`: int :param `userData`: :type `userData`: PyUserData :rtype: :ref:`SizerItem` **~~~** **Add** `(self, width, height, pos, span=DefaultSpan, flag=0, border=0, userData=None)` :param `width`: :type `width`: int :param `height`: :type `height`: int :param `pos`: :type `pos`: GBPosition :param `span`: :type `span`: GBSpan :param `flag`: :type `flag`: int :param `border`: :type `border`: int :param `userData`: :type `userData`: PyUserData :rtype: :ref:`SizerItem` **~~~** **Add** `(self, item)` :param `item`: :type `item`: GBSizerItem :rtype: :ref:`SizerItem` **~~~** .. method:: CalcMin(self) Called when the managed size of the sizer is needed or when layout needs done. :rtype: :ref:`Size` .. method:: CheckForIntersection(self, *args, **kw) Look at all items and see if any intersect (or would overlap) the given item. Returns ``True`` if so, ``False`` if there would be no overlap. If an `excludeItem` is given then it will not be checked for intersection, for example it may be the item we are checking the position of. |overload| **Overloaded Implementations**: **~~~** **CheckForIntersection** `(self, item, excludeItem=None)` :param `item`: :type `item`: GBSizerItem :param `excludeItem`: :type `excludeItem`: GBSizerItem :rtype: `bool` **~~~** **CheckForIntersection** `(self, pos, span, excludeItem=None)` :param `pos`: :type `pos`: GBPosition :param `span`: :type `span`: GBSpan :param `excludeItem`: :type `excludeItem`: GBSizerItem :rtype: `bool` **~~~** .. method:: FindItem(self, *args, **kw) Find the sizer item for the given window or subsizer, returns ``None`` if not found. (non-recursive) |overload| **Overloaded Implementations**: **~~~** **FindItem** `(self, window)` :param `window`: :type `window`: Window :rtype: :ref:`GBSizerItem` **~~~** **FindItem** `(self, sizer)` :param `sizer`: :type `sizer`: Sizer :rtype: :ref:`GBSizerItem` **~~~** .. method:: FindItemAtPoint(self, pt) Return the sizer item located at the point given in pt, or ``None`` if there is no item at that point. The (x,y) coordinates in `pt` correspond to the client coordinates of the window using the sizer for layout. (non-recursive) :param `pt`: :type `pt`: Point :rtype: :ref:`GBSizerItem` .. method:: FindItemAtPosition(self, pos) Return the sizer item for the given grid cell, or ``None`` if there is no item at that position. (non-recursive) :param `pos`: :type `pos`: GBPosition :rtype: :ref:`GBSizerItem` .. method:: FindItemWithData(self, userData) Return the sizer item that has a matching user data (it only compares pointer values) or ``None`` if not found. (non-recursive) :param `userData`: :type `userData`: Object :rtype: :ref:`GBSizerItem` .. method:: GetCellSize(self, row, col) Get the size of the specified cell, including hgap and vgap. Only valid after window layout has been performed. :param `row`: :type `row`: int :param `col`: :type `col`: int :rtype: :ref:`Size` .. method:: GetEmptyCellSize(self) Get the size used for cells in the grid with no item. :rtype: :ref:`Size` .. method:: GetItemPosition(self, *args, **kw) Get the grid position of the specified item. |overload| **Overloaded Implementations**: **~~~** **GetItemPosition** `(self, window)` :param `window`: :type `window`: Window :rtype: :ref:`GBPosition` **~~~** **GetItemPosition** `(self, sizer)` :param `sizer`: :type `sizer`: Sizer :rtype: :ref:`GBPosition` **~~~** **GetItemPosition** `(self, index)` :param `index`: :type `index`: int :rtype: :ref:`GBPosition` **~~~** .. method:: GetItemSpan(self, *args, **kw) Get the row/col spanning of the specified item. |overload| **Overloaded Implementations**: **~~~** **GetItemSpan** `(self, window)` :param `window`: :type `window`: Window :rtype: :ref:`GBSpan` **~~~** **GetItemSpan** `(self, sizer)` :param `sizer`: :type `sizer`: Sizer :rtype: :ref:`GBSpan` **~~~** **GetItemSpan** `(self, index)` :param `index`: :type `index`: int :rtype: :ref:`GBSpan` **~~~** .. method:: RecalcSizes(self) Called when the managed size of the sizer is needed or when layout needs done. .. method:: SetEmptyCellSize(self, sz) Set the size used for cells in the grid with no item. :param `sz`: :type `sz`: Size .. method:: SetItemPosition(self, *args, **kw) Set the grid position of the specified item. Returns ``True`` on success. If the move is not allowed (because an item is already there) then ``False`` is returned. |overload| **Overloaded Implementations**: **~~~** **SetItemPosition** `(self, window, pos)` :param `window`: :type `window`: Window :param `pos`: :type `pos`: GBPosition :rtype: `bool` **~~~** **SetItemPosition** `(self, sizer, pos)` :param `sizer`: :type `sizer`: Sizer :param `pos`: :type `pos`: GBPosition :rtype: `bool` **~~~** **SetItemPosition** `(self, index, pos)` :param `index`: :type `index`: int :param `pos`: :type `pos`: GBPosition :rtype: `bool` **~~~** .. method:: SetItemSpan(self, *args, **kw) Set the row/col spanning of the specified item. Returns ``True`` on success. If the move is not allowed (because an item is already there) then ``False`` is returned. |overload| **Overloaded Implementations**: **~~~** **SetItemSpan** `(self, window, span)` :param `window`: :type `window`: Window :param `span`: :type `span`: GBSpan :rtype: `bool` **~~~** **SetItemSpan** `(self, sizer, span)` :param `sizer`: :type `sizer`: Sizer :param `span`: :type `span`: GBSpan :rtype: `bool` **~~~** **SetItemSpan** `(self, index, span)` :param `index`: :type `index`: int :param `span`: :type `span`: GBSpan :rtype: `bool` **~~~** .. attribute:: EmptyCellSize See :meth:`~GridBagSizer.GetEmptyCellSize` and :meth:`~GridBagSizer.SetEmptyCellSize`