]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import wx # This module uses the new wx namespace | |
3 | ||
4 | #---------------------------------------------------------------------- | |
5 | gbsDescription = """\ | |
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. | |
9 | """ | |
10 | ||
11 | ||
12 | class TestFrame(wx.Frame): | |
13 | def __init__(self): | |
14 | wx.Frame.__init__(self, None, -1, "wx.GridBagSizer") | |
15 | p = wx.Panel(self, -1) | |
16 | p.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) | |
17 | ||
18 | gbs = self.gbs = wx.GridBagSizer(5, 5) | |
19 | ||
20 | gbs.Add( wx.StaticText(p, -1, gbsDescription), | |
21 | (0,0), (1,7), wx.ALIGN_CENTER | wx.ALL, 5) | |
22 | ||
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) ) | |
27 | ||
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 ) | |
30 | ||
31 | gbs.Add( wx.TextCtrl(p, -1, "pos(4,3), span(3,1)", style=wx.TE_MULTILINE), | |
32 | (4,3), (3,1), wx.EXPAND) | |
33 | ||
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) ) | |
37 | ||
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) ) | |
42 | ||
43 | hideBtn = wx.Button(p, -1, "Hide this item -->") | |
44 | gbs.Add(hideBtn, (12, 3)) | |
45 | ||
46 | hideTxt = wx.TextCtrl(p, -1, "pos(12,4), size(150, -1)", size = (150,-1)) | |
47 | gbs.Add( hideTxt, (12,4) ) | |
48 | ||
49 | showBtn = wx.Button(p, -1, "<-- Show it again") | |
50 | gbs.Add(showBtn, (12, 5)) | |
51 | showBtn.Disable() | |
52 | self.hideBtn = hideBtn | |
53 | self.showBtn = showBtn | |
54 | self.hideTxt = hideTxt | |
55 | ||
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) | |
60 | ||
61 | # Add a spacer at the end to ensure some extra space at the bottom | |
62 | gbs.Add((10,10), (14,7)) | |
63 | ||
64 | gbs.AddGrowableRow(3) | |
65 | gbs.AddGrowableCol(2) | |
66 | ||
67 | p.SetSizerAndFit(gbs) | |
68 | self.SetClientSize(p.GetSize()) | |
69 | ||
70 | ||
71 | def OnHideButton(self, evt): | |
72 | self.gbs.Hide(self.hideTxt) | |
73 | self.hideBtn.Disable() | |
74 | self.showBtn.Enable() | |
75 | self.gbs.Layout() | |
76 | ||
77 | ||
78 | def OnShowButton(self, evt): | |
79 | self.gbs.Show(self.hideTxt) | |
80 | self.hideBtn.Enable() | |
81 | self.showBtn.Disable() | |
82 | self.gbs.Layout() | |
83 | ||
84 | ||
85 | def OnMoveButton(self, evt): | |
86 | btn = evt.GetEventObject() | |
87 | curPos = self.gbs.GetItemPosition(btn) | |
88 | ||
89 | # if it's already at the "other" spot then move it back | |
90 | if curPos == (3,6): | |
91 | self.gbs.SetItemPosition(btn, self.lastPos) | |
92 | btn.SetLabel("Move this to (3,6)") | |
93 | else: | |
94 | if self.gbs.CheckForIntersectionPos( (3,6), (1,1) ): | |
95 | wx.MessageBox("""\ | |
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) | |
100 | ||
101 | try: | |
102 | if self.gbs.SetItemPosition(btn, (3,6)): | |
103 | self.lastPos = curPos | |
104 | btn.SetLabel("Move it back") | |
105 | except wx.PyAssertionError: | |
106 | pass | |
107 | ||
108 | self.gbs.Layout() | |
109 | ||
110 | ||
111 | def OnLeftDown(self, evt): | |
112 | pt = evt.GetPosition() | |
113 | item = self.gbs.FindItemAtPoint(pt) | |
114 | if item is None: | |
115 | print "no item at", `pt` | |
116 | else: | |
117 | print "item found: ", `item.GetPos()`, "--", `item.GetSpan()` | |
118 | ||
119 | ||
120 | #--------------------------------------------------------------------------- | |
121 | ||
122 | class TestPanel(wx.Panel): | |
123 | def __init__(self, parent, log): | |
124 | self.log = log | |
125 | wx.Panel.__init__(self, parent, -1) | |
126 | ||
127 | b = wx.Button(self, -1, "Show the GridBagSizer sample", (50,50)) | |
128 | self.Bind(wx.EVT_BUTTON, self.OnButton, b) | |
129 | ||
130 | ||
131 | def OnButton(self, evt): | |
132 | win = TestFrame() | |
133 | win.Show(True) | |
134 | ||
135 | ||
136 | ||
137 | #--------------------------------------------------------------------------- | |
138 | ||
139 | ||
140 | def runTest(frame, nb, log): | |
141 | win = TestPanel(nb, log) | |
142 | return win | |
143 | ||
144 | ||
145 | #---------------------------------------------------------------------- | |
146 | ||
147 | ||
148 | ||
149 | overview = """<html><body> | |
150 | <h2><center>wx.GridBagSizer</center></h2> | |
151 | ||
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. | |
156 | </body></html> | |
157 | """ | |
158 | ||
159 | ||
160 | ||
161 | if __name__ == '__main__': | |
162 | import sys,os | |
163 | import run | |
164 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) | |
165 |