]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridBagSizer.py
   2 import wx                  
# This module uses the new wx namespace 
   4 #---------------------------------------------------------------------- 
   6 The wx.GridBagSizer is similar to the wx.FlexGridSizer except the items are explicitly positioned 
   7 in a virtual cell of the layout grid, and column or row spanning is allowed.  For example, this 
   8 static text is positioned at (0,0) and it spans 7 columns. 
  12 class TestFrame(wx
.Frame
): 
  14         wx
.Frame
.__init
__(self
, None, -1, "wx.GridBagSizer") 
  15         p 
= wx
.Panel(self
, -1, style 
= wx
.TAB_TRAVERSAL
 
  17                      | wx
.FULL_REPAINT_ON_RESIZE
 
  19         p
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeftDown
) 
  21         gbs 
= self
.gbs 
= wx
.GridBagSizer(5, 5) 
  23         gbs
.Add( wx
.StaticText(p
, -1, gbsDescription
), 
  24                  (0,0), (1,7), wx
.ALIGN_CENTER | wx
.ALL
, 5) 
  26         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(1,0)"), (1,0) ) 
  27         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(1,1)"), (1,1) ) 
  28         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(2,0)"), (2,0) ) 
  29         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(2,1)"), (2,1) ) 
  31         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(3,2), span(1,2)\nthis row and col are growable", style
=wx
.TE_MULTILINE
), 
  32                  (3,2), (1,2), flag
=wx
.EXPAND 
) 
  34         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(4,3), span(3,1)", style
=wx
.TE_MULTILINE
), 
  35                  (4,3), (3,1), wx
.EXPAND
) 
  37         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(5,4)"), (5,4), flag
=wx
.EXPAND 
) 
  38         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(6,5)"), (6,5), flag
=wx
.EXPAND 
) 
  39         gbs
.Add( wx
.TextCtrl(p
, -1, "pos(7,6)"), (7,6) ) 
  41         moveBtn1 
= wx
.Button(p
, -1, "Move this to (3,6)") 
  42         moveBtn2 
= wx
.Button(p
, -1, "Move this to (3,6)"); 
  43         gbs
.Add( moveBtn1
, (10,2) ) 
  44         gbs
.Add( moveBtn2
, (10,3) ) 
  46         hideBtn 
= wx
.Button(p
, -1, "Hide this item -->") 
  47         gbs
.Add(hideBtn
, (12, 3)) 
  49         hideTxt 
= wx
.TextCtrl(p
, -1, "pos(12,4), size(150, -1)", size 
= (150,-1)) 
  50         gbs
.Add( hideTxt
, (12,4) ) 
  52         showBtn 
= wx
.Button(p
, -1, "<-- Show it again") 
  53         gbs
.Add(showBtn
, (12, 5)) 
  55         self
.hideBtn 
= hideBtn
 
  56         self
.showBtn 
= showBtn
 
  57         self
.hideTxt 
= hideTxt
 
  59         self
.Bind(wx
.EVT_BUTTON
, self
.OnHideButton
, hideBtn
) 
  60         self
.Bind(wx
.EVT_BUTTON
, self
.OnShowButton
, showBtn
) 
  61         self
.Bind(wx
.EVT_BUTTON
, self
.OnMoveButton
, moveBtn1
) 
  62         self
.Bind(wx
.EVT_BUTTON
, self
.OnMoveButton
, moveBtn2
) 
  64         # Add a spacer at the end to ensure some extra space at the bottom 
  65         gbs
.Add((10,10), (14,7)) 
  71         self
.SetClientSize(p
.GetSize()) 
  74     def OnHideButton(self
, evt
): 
  75         self
.gbs
.Hide(self
.hideTxt
) 
  76         self
.hideBtn
.Disable() 
  81     def OnShowButton(self
, evt
): 
  82         self
.gbs
.Show(self
.hideTxt
) 
  84         self
.showBtn
.Disable() 
  88     def OnMoveButton(self
, evt
): 
  89         btn 
= evt
.GetEventObject() 
  90         curPos 
= self
.gbs
.GetItemPosition(btn
) 
  92         # if it's already at the "other" spot then move it back 
  94             self
.gbs
.SetItemPosition(btn
, self
.lastPos
) 
  95             btn
.SetLabel("Move this to (3,6)") 
  97             if self
.gbs
.CheckForIntersectionPos( (3,6), (1,1) ): 
  99 wx.GridBagSizer will not allow items to be in the same cell as 
 100 another item, so this operation will fail.  You will also get an 
 101 assert when compiled in debug mode.""", 
 102                               "Warning", wx
.OK | wx
.ICON_INFORMATION
) 
 105                 if self
.gbs
.SetItemPosition(btn
, (3,6)): 
 106                     self
.lastPos 
= curPos
 
 107                     btn
.SetLabel("Move it back") 
 108             except wx
.PyAssertionError
: 
 114     def OnLeftDown(self
, evt
): 
 115         pt 
= evt
.GetPosition() 
 116         item 
= self
.gbs
.FindItemAtPoint(pt
) 
 118             print "no item at", `pt`
 
 120             print "item found: ", `item
.GetPos()`
, "--", `item
.GetSpan()`
 
 123 #--------------------------------------------------------------------------- 
 125 class TestPanel(wx
.Panel
): 
 126     def __init__(self
, parent
, log
): 
 128         wx
.Panel
.__init
__(self
, parent
, -1) 
 130         b 
= wx
.Button(self
, -1, "Show the GridBagSizer sample", (50,50)) 
 131         self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
) 
 134     def OnButton(self
, evt
): 
 140 #--------------------------------------------------------------------------- 
 143 def runTest(frame
, nb
, log
): 
 144     win 
= TestPanel(nb
, log
) 
 148 #---------------------------------------------------------------------- 
 152 overview 
= """<html><body> 
 153 <h2><center>wx.GridBagSizer</center></h2> 
 155 The wx.GridBagSizer is more or less a port of the the RowColSizer (that 
 156 has been in the wxPython.lib package for quite some time) to C++.  It 
 157 allows items to be placed at specific layout grid cells, and items can 
 158 span across more than one row or column. 
 164 if __name__ 
== '__main__': 
 167     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])