Table Of Contents

Previous topic

GregorianAdoption

Next topic

GridSizer

This Page

phoenix_title GridBagSizer

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.


class_hierarchy Inheritance Diagram

Inheritance diagram for class GridBagSizer

Inheritance diagram of GridBagSizer


user Contributed Examples

Example 1 - Chris Barker (download):

#!/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()

Example 2 - Chris Barker (download):

#!/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()

method_summary Methods Summary

__init__ Constructor, with optional parameters to specify the gap between the rows and columns.
Add Adds the given item to the given position.
CalcMin Called when the managed size of the sizer is needed or when layout needs done.
CheckForIntersection Look at all items and see if any intersect (or would overlap) the given item.
FindItem Find the sizer item for the given window or subsizer, returns None if not found.
FindItemAtPoint Return the sizer item located at the point given in pt, or None if there is no item at that point.
FindItemAtPosition Return the sizer item for the given grid cell, or None if there is no item at that position.
FindItemWithData Return the sizer item that has a matching user data (it only compares pointer values) or None if not found.
GetCellSize Get the size of the specified cell, including hgap and vgap.
GetEmptyCellSize Get the size used for cells in the grid with no item.
GetItemPosition Get the grid position of the specified item.
GetItemSpan Get the row/col spanning of the specified item.
RecalcSizes Called when the managed size of the sizer is needed or when layout needs done.
SetEmptyCellSize Set the size used for cells in the grid with no item.
SetItemPosition Set the grid position of the specified item.
SetItemSpan Set the row/col spanning of the specified item.

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)

Methods



__init__(self, vgap=0, hgap=0)

Constructor, with optional parameters to specify the gap between the rows and columns.

Parameters:
  • vgap (int) –
  • hgap (int) –


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)

Parameters:
  • window (Window) –
  • pos (GBPosition) –
  • span (GBSpan) –
  • flag (int) –
  • border (int) –
  • userData (PyUserData) –
Return type:

SizerItem



Add (self, sizer, pos, span=DefaultSpan, flag=0, border=0, userData=None)

Parameters:
  • sizer (Sizer) –
  • pos (GBPosition) –
  • span (GBSpan) –
  • flag (int) –
  • border (int) –
  • userData (PyUserData) –
Return type:

SizerItem



Add (self, width, height, pos, span=DefaultSpan, flag=0, border=0, userData=None)

Parameters:
  • width (int) –
  • height (int) –
  • pos (GBPosition) –
  • span (GBSpan) –
  • flag (int) –
  • border (int) –
  • userData (PyUserData) –
Return type:

SizerItem



Add (self, item)

Parameters:item (GBSizerItem) –
Return type: SizerItem





CalcMin(self)

Called when the managed size of the sizer is needed or when layout needs done.

Return type: Size


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)

Parameters:
Return type:

bool



CheckForIntersection (self, pos, span, excludeItem=None)

Parameters:
Return type:

bool





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)

Parameters:window (Window) –
Return type: GBSizerItem



FindItem (self, sizer)

Parameters:sizer (Sizer) –
Return type: GBSizerItem





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)

Parameters:pt (Point) –
Return type: GBSizerItem


FindItemAtPosition(self, pos)

Return the sizer item for the given grid cell, or None if there is no item at that position.

(non-recursive)

Parameters:pos (GBPosition) –
Return type: GBSizerItem


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)

Parameters:userData (Object) –
Return type: GBSizerItem


GetCellSize(self, row, col)

Get the size of the specified cell, including hgap and vgap.

Only valid after window layout has been performed.

Parameters:
  • row (int) –
  • col (int) –
Return type:

Size



GetEmptyCellSize(self)

Get the size used for cells in the grid with no item.

Return type: Size


GetItemPosition(self, *args, **kw)

Get the grid position of the specified item.


overload Overloaded Implementations:



GetItemPosition (self, window)

Parameters:window (Window) –
Return type: GBPosition



GetItemPosition (self, sizer)

Parameters:sizer (Sizer) –
Return type: GBPosition



GetItemPosition (self, index)

Parameters:index (int) –
Return type: GBPosition





GetItemSpan(self, *args, **kw)

Get the row/col spanning of the specified item.


overload Overloaded Implementations:



GetItemSpan (self, window)

Parameters:window (Window) –
Return type: GBSpan



GetItemSpan (self, sizer)

Parameters:sizer (Sizer) –
Return type: GBSpan



GetItemSpan (self, index)

Parameters:index (int) –
Return type: GBSpan





RecalcSizes(self)

Called when the managed size of the sizer is needed or when layout needs done.



SetEmptyCellSize(self, sz)

Set the size used for cells in the grid with no item.

Parameters:sz (Size) –


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)

Parameters:
Return type:

bool



SetItemPosition (self, sizer, pos)

Parameters:
Return type:

bool



SetItemPosition (self, index, pos)

Parameters:
Return type:

bool





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)

Parameters:
Return type:

bool



SetItemSpan (self, sizer, span)

Parameters:
Return type:

bool



SetItemSpan (self, index, span)

Parameters:
  • index (int) –
  • span (GBSpan) –
Return type:

bool




Properties



EmptyCellSize

See GetEmptyCellSize and SetEmptyCellSize