]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxGridBagSizer.py
Fixed the code to actually work.
[wxWidgets.git] / wxPython / demo / wxGridBagSizer.py
CommitLineData
d14a1e28
RD
1
2import wx # This module uses the new wx namespace
3
4#----------------------------------------------------------------------
5gbsDescription = """\
6The wxGridBagSizer is similar to the wxFlexGridSizer except the items are explicitly positioned
7in a virtual cell of the layout grid, and column or row spanning is allowed. For example, this
8static text is positioned at (0,0) and it spans 7 columns.
9"""
10
11
12class TestFrame(wx.Frame):
13 def __init__(self):
14 wx.Frame.__init__(self, None, -1, "wxGridBagSizer")
15 p = wx.Panel(self, -1)
16 gbs = self.gbs = wx.GridBagSizer()
17
18 gbs.Add( wx.StaticText(p, -1, gbsDescription),
19 (0,0), (1,7), wx.ALIGN_CENTER | wx.ALL, 5)
20
21
22 gbs.Add( wx.TextCtrl(p, -1, "pos(1,0)"), (1,0) )
23 gbs.Add( wx.TextCtrl(p, -1, "pos(1,1)"), (1,1) )
24 gbs.Add( wx.TextCtrl(p, -1, "pos(2,0)"), (2,0) )
25 gbs.Add( wx.TextCtrl(p, -1, "pos(2,1)"), (2,1) )
26
27 gbs.Add( wx.TextCtrl(p, -1, "pos(3,2), span(1,2)\nthis row and col are growable", style=wx.TE_MULTILINE),
28 (3,2), (1,2), flag=wx.EXPAND )
29
30 gbs.Add( wx.TextCtrl(p, -1, "pos(4,3), span(3,1)", style=wx.TE_MULTILINE),
31 (4,3), (3,1), wx.EXPAND)
32
33 gbs.Add( wx.TextCtrl(p, -1, "pos(5,4)"), (5,4), flag=wx.EXPAND )
34 gbs.Add( wx.TextCtrl(p, -1, "pos(6,5)"), (6,5), flag=wx.EXPAND )
35 gbs.Add( wx.TextCtrl(p, -1, "pos(7,6)"), (7,6) )
36
37 moveBtn1 = wx.Button(p, -1, "Move this to (3,6)")
38 moveBtn2 = wx.Button(p, -1, "Move this to (3,6)");
39 gbs.Add( moveBtn1, (10,2) )
40 gbs.Add( moveBtn2, (10,3) )
41
42 hideBtn = wx.Button(p, -1, "Hide this item -->")
43 gbs.Add(hideBtn, (12, 3))
44
45 hideTxt = wx.TextCtrl(p, -1, "pos(12,4), size(150, -1)", size = (150,-1))
46 gbs.Add( hideTxt, (12,4) )
47
48 showBtn = wx.Button(p, -1, "<-- Show it again")
49 gbs.Add(showBtn, (12, 5))
50 showBtn.Disable()
51 self.hideBtn = hideBtn
52 self.showBtn = showBtn
53 self.hideTxt = hideTxt
54
55 self.Bind(wx.EVT_BUTTON, self.OnHideButton, hideBtn)
56 self.Bind(wx.EVT_BUTTON, self.OnShowButton, showBtn)
57 self.Bind(wx.EVT_BUTTON, self.OnMoveButton, moveBtn1)
58 self.Bind(wx.EVT_BUTTON, self.OnMoveButton, moveBtn2)
59
60 # Add a spacer at the end to ensure some extra space at the bottom
61 gbs.Add((10,10), (14,0))
62
63 gbs.AddGrowableRow(3)
64 gbs.AddGrowableCol(2)
65
66 p.SetSizerAndFit(gbs)
67 self.SetClientSize(p.GetSize())
68
69
70 def OnHideButton(self, evt):
71 self.gbs.Hide(self.hideTxt)
72 self.hideBtn.Disable()
73 self.showBtn.Enable()
74 self.gbs.Layout()
75
76
77 def OnShowButton(self, evt):
78 self.gbs.Show(self.hideTxt)
79 self.hideBtn.Enable()
80 self.showBtn.Disable()
81 self.gbs.Layout()
82
83
84 def OnMoveButton(self, evt):
85 btn = evt.GetEventObject()
86 curPos = self.gbs.GetItemPosition(btn)
87
88 # if it's already at the "other" spot then move it back
89 if curPos == (3,6):
90 self.gbs.SetItemPosition(btn, self.lastPos)
91 btn.SetLabel("Move this to (3,6)")
92 else:
93 if self.gbs.CheckForIntersection( (3,6), (1,1) ):
94 wx.MessageBox("""\
95wxGridBagSizer will not allow items to be in the same cell as
96another item, so this operation will fail. You will also get an assert
97when compiled in debug mode.""",
98 "Warning", wx.OK | wx.ICON_INFORMATION)
99
100 try:
101 if self.gbs.SetItemPosition(btn, (3,6)):
102 self.lastPos = curPos
103 btn.SetLabel("Move it back")
104 except wx.PyAssertionError:
105 pass
106
107 self.gbs.Layout()
108
109
110#----------------------------------------------------------------------
111
112def runTest(frame, nb, log):
113 win = TestFrame()
114 frame.otherWin = win
115 win.Show(True)
116
117
118#----------------------------------------------------------------------
119
120
121
122overview = """<html><body>
123<h2><center>wxGridBagSizer</center></h2>
124
125The wxGridBagSizer is more or less a port of the the RowColSizer (that
126has been in the wxPython.lib package for quite some time) to C++. It
127allows items to be placed at specific layout grid cells, and items can
128span across more than one row or column.
129</body></html>
130"""
131
132
133
134if __name__ == '__main__':
135 import sys,os
136 import run
137 run.main(['', os.path.basename(sys.argv[0])])
138