]>
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)
16 p
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeftDown
)
18 gbs
= self
.gbs
= wx
.GridBagSizer(5, 5)
20 gbs
.Add( wx
.StaticText(p
, -1, gbsDescription
),
21 (0,0), (1,7), wx
.ALIGN_CENTER | wx
.ALL
, 5)
23 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(1,0)"), (1,0) )
24 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(1,1)"), (1,1) )
25 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(2,0)"), (2,0) )
26 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(2,1)"), (2,1) )
28 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(3,2), span(1,2)\nthis row and col are growable", style
=wx
.TE_MULTILINE
),
29 (3,2), (1,2), flag
=wx
.EXPAND
)
31 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(4,3), span(3,1)", style
=wx
.TE_MULTILINE
),
32 (4,3), (3,1), wx
.EXPAND
)
34 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(5,4)"), (5,4), flag
=wx
.EXPAND
)
35 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(6,5)"), (6,5), flag
=wx
.EXPAND
)
36 gbs
.Add( wx
.TextCtrl(p
, -1, "pos(7,6)"), (7,6) )
38 moveBtn1
= wx
.Button(p
, -1, "Move this to (3,6)")
39 moveBtn2
= wx
.Button(p
, -1, "Move this to (3,6)");
40 gbs
.Add( moveBtn1
, (10,2) )
41 gbs
.Add( moveBtn2
, (10,3) )
43 hideBtn
= wx
.Button(p
, -1, "Hide this item -->")
44 gbs
.Add(hideBtn
, (12, 3))
46 hideTxt
= wx
.TextCtrl(p
, -1, "pos(12,4), size(150, -1)", size
= (150,-1))
47 gbs
.Add( hideTxt
, (12,4) )
49 showBtn
= wx
.Button(p
, -1, "<-- Show it again")
50 gbs
.Add(showBtn
, (12, 5))
52 self
.hideBtn
= hideBtn
53 self
.showBtn
= showBtn
54 self
.hideTxt
= hideTxt
56 self
.Bind(wx
.EVT_BUTTON
, self
.OnHideButton
, hideBtn
)
57 self
.Bind(wx
.EVT_BUTTON
, self
.OnShowButton
, showBtn
)
58 self
.Bind(wx
.EVT_BUTTON
, self
.OnMoveButton
, moveBtn1
)
59 self
.Bind(wx
.EVT_BUTTON
, self
.OnMoveButton
, moveBtn2
)
61 # Add a spacer at the end to ensure some extra space at the bottom
62 gbs
.Add((10,10), (14,7))
68 self
.SetClientSize(p
.GetSize())
71 def OnHideButton(self
, evt
):
72 self
.gbs
.Hide(self
.hideTxt
)
73 self
.hideBtn
.Disable()
78 def OnShowButton(self
, evt
):
79 self
.gbs
.Show(self
.hideTxt
)
81 self
.showBtn
.Disable()
85 def OnMoveButton(self
, evt
):
86 btn
= evt
.GetEventObject()
87 curPos
= self
.gbs
.GetItemPosition(btn
)
89 # if it's already at the "other" spot then move it back
91 self
.gbs
.SetItemPosition(btn
, self
.lastPos
)
92 btn
.SetLabel("Move this to (3,6)")
94 if self
.gbs
.CheckForIntersectionPos( (3,6), (1,1) ):
96 wx.GridBagSizer will not allow items to be in the same cell as
97 another item, so this operation will fail. You will also get an
98 assert when compiled in debug mode.""",
99 "Warning", wx
.OK | wx
.ICON_INFORMATION
)
102 if self
.gbs
.SetItemPosition(btn
, (3,6)):
103 self
.lastPos
= curPos
104 btn
.SetLabel("Move it back")
105 except wx
.PyAssertionError
:
111 def OnLeftDown(self
, evt
):
112 pt
= evt
.GetPosition()
113 item
= self
.gbs
.FindItemAtPoint(pt
)
115 print "no item at", `pt`
117 print "item found: ", `item
.GetPos()`
, "--", `item
.GetSpan()`
120 #---------------------------------------------------------------------------
122 class TestPanel(wx
.Panel
):
123 def __init__(self
, parent
, log
):
125 wx
.Panel
.__init
__(self
, parent
, -1)
127 b
= wx
.Button(self
, -1, "Show the GridBagSizer sample", (50,50))
128 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
131 def OnButton(self
, evt
):
137 #---------------------------------------------------------------------------
140 def runTest(frame
, nb
, log
):
141 win
= TestPanel(nb
, log
)
145 #----------------------------------------------------------------------
149 overview
= """<html><body>
150 <h2><center>wx.GridBagSizer</center></h2>
152 The wx.GridBagSizer is more or less a port of the the RowColSizer (that
153 has been in the wxPython.lib package for quite some time) to C++. It
154 allows items to be placed at specific layout grid cells, and items can
155 span across more than one row or column.
161 if __name__
== '__main__':
164 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])