]>
Commit | Line | Data |
---|---|---|
2f90df85 RD |
1 | #---------------------------------------------------------------------- |
2 | # sizer test code | |
3 | #---------------------------------------------------------------------- | |
4 | ||
5 | from wxPython.wx import * | |
6 | from wxPython.lib.sizers import * | |
7 | ||
8 | #---------------------------------------------------------------------- | |
9 | ||
10 | def makeSimpleBox1(win): | |
11 | box = wxBoxSizer(wxHORIZONTAL) | |
12 | box.Add(wxButton(win, 1010, "one"), 0) | |
13 | box.Add(wxButton(win, 1010, "two"), 0) | |
14 | box.Add(wxButton(win, 1010, "three"), 0) | |
15 | box.Add(wxButton(win, 1010, "four"), 0) | |
16 | ||
17 | return box | |
18 | ||
19 | #---------------------------------------------------------------------- | |
20 | ||
21 | def makeSimpleBox2(win): | |
22 | box = wxBoxSizer(wxVERTICAL) | |
23 | box.Add(wxButton(win, 1010, "one"), 0) | |
24 | box.Add(wxButton(win, 1010, "two"), 0) | |
25 | box.Add(wxButton(win, 1010, "three"), 0) | |
26 | box.Add(wxButton(win, 1010, "four"), 0) | |
27 | ||
28 | return box | |
29 | ||
30 | #---------------------------------------------------------------------- | |
31 | ||
32 | def makeSimpleBox3(win): | |
33 | box = wxBoxSizer(wxHORIZONTAL) | |
34 | box.Add(wxButton(win, 1010, "one"), 0) | |
35 | box.Add(wxButton(win, 1010, "two"), 0) | |
36 | box.Add(wxButton(win, 1010, "three"), 0) | |
37 | box.Add(wxButton(win, 1010, "four"), 0) | |
38 | box.Add(wxButton(win, 1010, "five"), 1) | |
39 | ||
40 | return box | |
41 | ||
42 | #---------------------------------------------------------------------- | |
43 | ||
44 | def makeSimpleBox4(win): | |
45 | box = wxBoxSizer(wxHORIZONTAL) | |
46 | box.Add(wxButton(win, 1010, "one"), 0) | |
47 | box.Add(wxButton(win, 1010, "two"), 0) | |
48 | box.Add(wxButton(win, 1010, "three"), 1) | |
49 | box.Add(wxButton(win, 1010, "four"), 1) | |
50 | box.Add(wxButton(win, 1010, "five"), 1) | |
51 | ||
52 | return box | |
53 | ||
54 | #---------------------------------------------------------------------- | |
55 | ||
56 | def makeSimpleBox5(win): | |
57 | box = wxBoxSizer(wxHORIZONTAL) | |
58 | box.Add(wxButton(win, 1010, "one"), 0) | |
59 | box.Add(wxButton(win, 1010, "two"), 0) | |
60 | box.Add(wxButton(win, 1010, "three"), 3) | |
61 | box.Add(wxButton(win, 1010, "four"), 1) | |
62 | box.Add(wxButton(win, 1010, "five"), 1) | |
63 | ||
64 | return box | |
65 | ||
66 | #---------------------------------------------------------------------- | |
67 | ||
68 | def makeSimpleBox6(win): | |
69 | box = wxBoxSizer(wxHORIZONTAL, wxSize(250, 50)) | |
70 | box.Add(wxButton(win, 1010, "10"), 10) | |
71 | box.Add(wxButton(win, 1010, "20"), 20) | |
72 | box.Add(wxButton(win, 1010, "30"), 30) | |
73 | box.Add(wxButton(win, 1010, "15"), 15) | |
74 | box.Add(wxButton(win, 1010, "5"), 5) | |
75 | ||
76 | return box | |
77 | ||
78 | #---------------------------------------------------------------------- | |
79 | ||
80 | def makeSimpleBorder1(win): | |
81 | bdr = wxBorderSizer(wxALL) | |
82 | btn = wxButton(win, 1010, "border") | |
83 | btn.SetSize(wxSize(80, 80)) | |
84 | bdr.Add(btn, 15) | |
85 | ||
86 | return bdr | |
87 | ||
88 | #---------------------------------------------------------------------- | |
89 | ||
90 | def makeSimpleBorder2(win): | |
91 | bdr = wxBorderSizer(wxEAST | wxWEST) | |
92 | btn = wxButton(win, 1010, "border") | |
93 | btn.SetSize(wxSize(80, 80)) | |
94 | bdr.Add(btn, 15) | |
95 | ||
96 | return bdr | |
97 | ||
98 | #---------------------------------------------------------------------- | |
99 | ||
100 | def makeSimpleBorder3(win): | |
101 | bdr = wxBorderSizer(wxNORTH | wxWEST) | |
102 | btn = wxButton(win, 1010, "border") | |
103 | btn.SetSize(wxSize(80, 80)) | |
104 | bdr.Add(btn, 15) | |
105 | ||
106 | return bdr | |
107 | ||
108 | #---------------------------------------------------------------------- | |
6147ee34 RD |
109 | |
110 | def makeShapes(win): | |
111 | box =wxBoxSizer(wxVERTICAL) | |
112 | box.Add(wxStaticLine(win, -1), 0) | |
113 | for line in ( | |
114 | (wxANCHOR_NW, "NorthWest"), | |
115 | (wxANCHOR_NORTH, "North"), | |
116 | (wxANCHOR_NE, "NorthEast") | |
117 | ), ( | |
118 | (wxANCHOR_WEST, "West"), | |
119 | (wxANCHOR_NONE, "Center"), | |
120 | (wxANCHOR_EAST, "East") | |
121 | ), ( | |
122 | (wxANCHOR_SW, "SouthWest"), | |
123 | (wxANCHOR_SOUTH, "South"), | |
124 | (wxANCHOR_SE, "SouthEast") | |
125 | ): | |
126 | linebox =wxBoxSizer(wxHORIZONTAL) | |
127 | linebox.Add(wxStaticLine(win, -1, style=wxVERTICAL), 0) | |
128 | for (anchor, label) in line: | |
129 | sizer =wxShapeSizer(anchor) | |
130 | sizer.Add(wxButton(win, -1, label, size=wxSize(100, 50))) | |
131 | linebox.Add(sizer, 1) | |
132 | linebox.Add(wxStaticLine(win, -1, style=wxVERTICAL), 0) | |
133 | box.Add(linebox, 1) | |
134 | box.Add(wxStaticLine(win, -1), 0) | |
135 | return box | |
136 | ||
2f90df85 RD |
137 | #---------------------------------------------------------------------- |
138 | ||
139 | def makeBoxInBox(win): | |
140 | box = wxBoxSizer(wxVERTICAL) | |
141 | ||
142 | box.Add(wxButton(win, 1010, "one")) | |
143 | ||
144 | box2 = wxBoxSizer(wxHORIZONTAL) | |
145 | box2.AddMany([ wxButton(win, 1010, "two"), | |
146 | wxButton(win, 1010, "three"), | |
147 | wxButton(win, 1010, "four"), | |
148 | wxButton(win, 1010, "five"), | |
149 | ]) | |
150 | ||
151 | box3 = wxBoxSizer(wxVERTICAL) | |
152 | box3.AddMany([ (wxButton(win, 1010, "six"), 0), | |
153 | (wxButton(win, 1010, "seven"), 2), | |
154 | (wxButton(win, 1010, "eight"), 1), | |
155 | (wxButton(win, 1010, "nine"), 1), | |
156 | ]) | |
157 | ||
158 | box2.Add(box3, 1) | |
159 | box.Add(box2, 1) | |
160 | ||
161 | box.Add(wxButton(win, 1010, "ten")) | |
162 | ||
163 | return box | |
164 | ||
165 | #---------------------------------------------------------------------- | |
166 | ||
167 | def makeBoxInBorder(win): | |
168 | bdr = wxBorderSizer(wxALL) | |
169 | box = makeSimpleBox3(win) | |
170 | bdr.Add(box, 15) | |
171 | ||
172 | return bdr | |
173 | ||
174 | #---------------------------------------------------------------------- | |
175 | ||
176 | def makeBorderInBox(win): | |
177 | insideBox = wxBoxSizer(wxHORIZONTAL) | |
178 | ||
179 | box2 = wxBoxSizer(wxHORIZONTAL) | |
180 | box2.AddMany([ wxButton(win, 1010, "one"), | |
181 | wxButton(win, 1010, "two"), | |
182 | wxButton(win, 1010, "three"), | |
183 | wxButton(win, 1010, "four"), | |
184 | wxButton(win, 1010, "five"), | |
185 | ]) | |
186 | ||
187 | insideBox.Add(box2, 0) | |
188 | ||
189 | bdr = wxBorderSizer(wxALL) | |
190 | bdr.Add(wxButton(win, 1010, "border"), 20) | |
191 | insideBox.Add(bdr, 1) | |
192 | ||
193 | box3 = wxBoxSizer(wxVERTICAL) | |
194 | box3.AddMany([ (wxButton(win, 1010, "six"), 0), | |
195 | (wxButton(win, 1010, "seven"), 2), | |
196 | (wxButton(win, 1010, "eight"), 1), | |
197 | (wxButton(win, 1010, "nine"), 1), | |
198 | ]) | |
199 | insideBox.Add(box3, 1) | |
200 | ||
201 | outsideBox = wxBoxSizer(wxVERTICAL) | |
202 | outsideBox.Add(wxButton(win, 1010, "top")) | |
203 | outsideBox.Add(insideBox, 1) | |
204 | outsideBox.Add(wxButton(win, 1010, "bottom")) | |
205 | ||
206 | return outsideBox | |
207 | ||
208 | ||
209 | #---------------------------------------------------------------------- | |
210 | ||
211 | theTests = [ | |
212 | ("Simple horizontal boxes", makeSimpleBox1, | |
213 | "This is a HORIZONTAL box sizer with four non-stretchable buttons held " | |
214 | "within it. Notice that the buttons are added and aligned in the horizontal " | |
215 | "dimension. Also notice that they are fixed size in the horizontal dimension, " | |
216 | "but will stretch vertically." | |
217 | ), | |
218 | ||
219 | ("Simple vertical boxes", makeSimpleBox2, | |
220 | "Exactly the same as the previous sample but using a VERTICAL box sizer " | |
221 | "instead of a HORIZONTAL one." | |
222 | ), | |
223 | ||
224 | ("Add a stretchable", makeSimpleBox3, | |
225 | "We've added one more button with the strechable flag turned on. Notice " | |
226 | "how it grows to fill the extra space in the otherwise fixed dimension." | |
227 | ), | |
228 | ||
229 | ("More than one stretchable", makeSimpleBox4, | |
230 | "Here there are several items that are stretchable, they all divide up the " | |
231 | "extra space evenly." | |
232 | ), | |
233 | ||
234 | ("Weighting factor", makeSimpleBox5, | |
235 | "This one shows more than one strechable, but one of them has a weighting " | |
236 | "factor so it gets more of the free space." | |
237 | ), | |
238 | ||
239 | # ("Percent Sizer", makeSimpleBox6, | |
240 | # "You can use the wxBoxSizer like a Percent Sizer. Just make sure that all " | |
241 | # "the weighting factors add up to 100!" | |
242 | # ), | |
243 | ||
244 | ("", None, ""), | |
245 | ||
246 | ("Simple border sizer", makeSimpleBorder1, | |
247 | "The wxBorderSizer leaves empty space around its contents. This one " | |
248 | "gives a border all the way around." | |
249 | ), | |
250 | ||
251 | ("East and West border", makeSimpleBorder2, | |
252 | "You can pick and choose which sides have borders." | |
253 | ), | |
254 | ||
255 | ("North and West border", makeSimpleBorder3, | |
256 | "You can pick and choose which sides have borders." | |
257 | ), | |
258 | ||
259 | ("", None, ""), | |
260 | ||
6147ee34 RD |
261 | |
262 | ("Proportional resize", makeShapes, | |
263 | "The wxShapeSizer preserves the original proportions of the window." | |
264 | ), | |
265 | ||
266 | ("", None, ""), | |
267 | ||
2f90df85 RD |
268 | ("Boxes inside of boxes", makeBoxInBox, |
269 | "This one shows nesting of boxes within boxes within boxes, using both " | |
270 | "orientations. Notice also that button seven has a greater weighting " | |
271 | "factor than its siblings." | |
272 | ), | |
273 | ||
274 | ("Boxes inside a Border", makeBoxInBorder, | |
275 | "Sizers of different types can be nested withing each other as well. " | |
276 | "Here is a box sizer with several buttons embedded within a border sizer." | |
277 | ), | |
278 | ||
279 | ("Border in a Box", makeBorderInBox, | |
280 | "Another nesting example. This one has Boxes and a Border inside another Box." | |
281 | ), | |
282 | ||
283 | ] | |
284 | #---------------------------------------------------------------------- | |
285 | ||
286 | class TestFrame(wxFrame): | |
287 | def __init__(self, parent, title, sizerFunc): | |
288 | wxFrame.__init__(self, parent, -1, title) | |
289 | EVT_BUTTON(self, 1010, self.OnButton) | |
290 | ||
291 | self.sizer = sizerFunc(self) | |
292 | self.CreateStatusBar() | |
293 | self.SetStatusText("Resize this frame to see how the sizers respond...") | |
294 | self.sizer.FitWindow(self) | |
f6bcfd97 BP |
295 | EVT_CLOSE(self, self.OnCloseWindow) |
296 | EVT_SIZE(self, self.OnSize) | |
2f90df85 RD |
297 | |
298 | ||
299 | def OnSize(self, event): | |
300 | size = self.GetClientSize() | |
301 | self.sizer.Layout(size) | |
302 | ||
303 | def OnCloseWindow(self, event): | |
304 | self.MakeModal(false) | |
305 | self.Destroy() | |
306 | ||
307 | def OnButton(self, event): | |
308 | self.Close(true) | |
309 | ||
310 | #---------------------------------------------------------------------- | |
311 | ||
312 | ||
313 | ||
314 | class TestSelectionPanel(wxPanel): | |
6147ee34 | 315 | def __init__(self, parent, frame=NULL): |
2f90df85 RD |
316 | wxPanel.__init__(self, parent, -1) |
317 | self.frame = frame | |
318 | ||
319 | self.list = wxListBox(self, 401, | |
320 | wxDLG_PNT(self, 10, 10), wxDLG_SZE(self, 100, 60), | |
321 | []) | |
322 | EVT_LISTBOX(self, 401, self.OnSelect) | |
323 | EVT_LISTBOX_DCLICK(self, 401, self.OnDClick) | |
324 | ||
325 | wxButton(self, 402, "Try it!", wxDLG_PNT(self, 120, 10)).SetDefault() | |
326 | EVT_BUTTON(self, 402, self.OnDClick) | |
327 | ||
328 | self.text = wxTextCtrl(self, -1, "", | |
329 | wxDLG_PNT(self, 10, 80), | |
330 | wxDLG_SZE(self, 200, 60), | |
331 | wxTE_MULTILINE | wxTE_READONLY) | |
332 | ||
333 | for item in theTests: | |
334 | self.list.Append(item[0]) | |
335 | ||
336 | ||
337 | ||
338 | def OnSelect(self, event): | |
339 | pos = self.list.GetSelection() | |
340 | self.text.SetValue(theTests[pos][2]) | |
341 | ||
342 | ||
343 | def OnDClick(self, event): | |
344 | pos = self.list.GetSelection() | |
345 | title = theTests[pos][0] | |
346 | func = theTests[pos][1] | |
347 | ||
348 | if func: | |
349 | win = TestFrame(self, title, func) | |
350 | win.CentreOnParent(wxBOTH) | |
351 | win.Show(true) | |
352 | win.MakeModal(true) | |
353 | ||
354 | #---------------------------------------------------------------------- | |
355 | ||
356 | def runTest(frame, nb, log): | |
357 | win = TestSelectionPanel(nb, frame) | |
358 | return win | |
359 | ||
360 | overview = wxSizer.__doc__ + '\n' + '-' * 80 + '\n' + \ | |
361 | wxBoxSizer.__doc__ + '\n' + '-' * 80 + '\n' + \ | |
362 | wxBorderSizer.__doc__ | |
363 | ||
364 | #---------------------------------------------------------------------- | |
365 | ||
366 | ||
367 | ||
368 | if __name__ == '__main__': | |
369 | ||
370 | class MainFrame(wxFrame): | |
371 | def __init__(self): | |
372 | wxFrame.__init__(self, NULL, -1, "Testing...") | |
373 | ||
374 | self.CreateStatusBar() | |
375 | mainmenu = wxMenuBar() | |
376 | menu = wxMenu() | |
377 | menu.Append(200, 'E&xit', 'Get the heck outta here!') | |
378 | mainmenu.Append(menu, "&File") | |
379 | self.SetMenuBar(mainmenu) | |
380 | EVT_MENU(self, 200, self.OnExit) | |
381 | self.panel = TestSelectionPanel(self, self) | |
382 | self.SetSize(wxSize(400, 380)) | |
f6bcfd97 | 383 | EVT_CLOSE(self, self.OnCloseWindow) |
2f90df85 RD |
384 | |
385 | def OnCloseWindow(self, event): | |
386 | self.Destroy() | |
387 | ||
388 | def OnExit(self, event): | |
389 | self.Close(true) | |
390 | ||
391 | ||
392 | class TestApp(wxApp): | |
393 | def OnInit(self): | |
394 | frame = MainFrame() | |
395 | frame.Show(true) | |
396 | self.SetTopWindow(frame) | |
397 | return true | |
398 | ||
399 | app = TestApp(0) | |
400 | app.MainLoop() | |
401 | ||
402 | ||
403 | #---------------------------------------------------------------------- |