1 #----------------------------------------------------------------------
3 #----------------------------------------------------------------------
5 from wxPython
.wx
import *
6 from wxPython
.lib
.grids
import wxGridSizer
, wxFlexGridSizer
8 #----------------------------------------------------------------------
10 def makeSimpleBox1(win
):
11 box
= wxBoxSizer(wxHORIZONTAL
)
12 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
13 box
.Add(wxButton(win
, 1010, "two"), 0, wxEXPAND
)
14 box
.Add(wxButton(win
, 1010, "three"), 0, wxEXPAND
)
15 box
.Add(wxButton(win
, 1010, "four"), 0, wxEXPAND
)
19 #----------------------------------------------------------------------
21 def makeSimpleBox2(win
):
22 box
= wxBoxSizer(wxVERTICAL
)
23 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
24 box
.Add(wxButton(win
, 1010, "two"), 0, wxEXPAND
)
25 box
.Add(wxButton(win
, 1010, "three"), 0, wxEXPAND
)
26 box
.Add(wxButton(win
, 1010, "four"), 0, wxEXPAND
)
30 #----------------------------------------------------------------------
32 def makeSimpleBox3(win
):
33 box
= wxBoxSizer(wxHORIZONTAL
)
34 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
35 box
.Add(wxButton(win
, 1010, "two"), 0, wxEXPAND
)
36 box
.Add(wxButton(win
, 1010, "three"), 0, wxEXPAND
)
37 box
.Add(wxButton(win
, 1010, "four"), 0, wxEXPAND
)
38 box
.Add(wxButton(win
, 1010, "five"), 1, wxEXPAND
)
42 #----------------------------------------------------------------------
44 def makeSimpleBox4(win
):
45 box
= wxBoxSizer(wxHORIZONTAL
)
46 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
47 box
.Add(wxButton(win
, 1010, "two"), 0, wxEXPAND
)
48 box
.Add(wxButton(win
, 1010, "three"), 1, wxEXPAND
)
49 box
.Add(wxButton(win
, 1010, "four"), 1, wxEXPAND
)
50 box
.Add(wxButton(win
, 1010, "five"), 1, wxEXPAND
)
54 #----------------------------------------------------------------------
56 def makeSimpleBox5(win
):
57 box
= wxBoxSizer(wxHORIZONTAL
)
58 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
59 box
.Add(wxButton(win
, 1010, "two"), 0, wxEXPAND
)
60 box
.Add(wxButton(win
, 1010, "three"), 3, wxEXPAND
)
61 box
.Add(wxButton(win
, 1010, "four"), 1, wxEXPAND
)
62 box
.Add(wxButton(win
, 1010, "five"), 1, wxEXPAND
)
66 #----------------------------------------------------------------------
68 def makeSimpleBox6(win
):
69 box
= wxBoxSizer(wxHORIZONTAL
)
70 box
.Add(wxButton(win
, 1010, "one"), 1, wxALIGN_TOP
)
71 box
.Add(wxButton(win
, 1010, "two"), 1, wxEXPAND
)
72 box
.Add(wxButton(win
, 1010, "three"), 1, wxALIGN_CENTER
)
73 box
.Add(wxButton(win
, 1010, "four"), 1, wxEXPAND
)
74 box
.Add(wxButton(win
, 1010, "five"), 1, wxALIGN_BOTTOM
)
78 #----------------------------------------------------------------------
80 def makeSimpleBox7(win
):
81 box
= wxBoxSizer(wxHORIZONTAL
)
82 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
83 box
.Add(wxButton(win
, 1010, "two"), 0, wxEXPAND
)
84 box
.Add(wxButton(win
, 1010, "three"), 0, wxEXPAND
)
85 box
.Add(60, 20, 0, wxEXPAND
)
86 box
.Add(wxButton(win
, 1010, "five"), 1, wxEXPAND
)
90 #----------------------------------------------------------------------
92 def makeSimpleBox8(win
):
93 box
= wxBoxSizer(wxVERTICAL
)
94 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
96 box
.Add(wxButton(win
, 1010, "two"), 0, wxALIGN_CENTER
)
98 box
.Add(wxButton(win
, 1010, "three"), 0, wxEXPAND
)
99 box
.Add(wxButton(win
, 1010, "four"), 0, wxEXPAND
)
100 # box.Add(wxButton(win, 1010, "five"), 1, wxEXPAND)
104 #----------------------------------------------------------------------
105 #----------------------------------------------------------------------
107 def makeSimpleBorder1(win
):
108 bdr
= wxBoxSizer(wxHORIZONTAL
)
109 btn
= wxButton(win
, 1010, "border")
110 btn
.SetSize(wxSize(80, 80))
111 bdr
.Add(btn
, 1, wxEXPAND|wxALL
, 15)
115 #----------------------------------------------------------------------
117 def makeSimpleBorder2(win
):
118 bdr
= wxBoxSizer(wxHORIZONTAL
)
119 btn
= wxButton(win
, 1010, "border")
120 btn
.SetSize(wxSize(80, 80))
121 bdr
.Add(btn
, 1, wxEXPAND | wxEAST | wxWEST
, 15)
125 #----------------------------------------------------------------------
127 def makeSimpleBorder3(win
):
128 bdr
= wxBoxSizer(wxHORIZONTAL
)
129 btn
= wxButton(win
, 1010, "border")
130 btn
.SetSize(wxSize(80, 80))
131 bdr
.Add(btn
, 1, wxEXPAND | wxNORTH | wxWEST
, 15)
135 #----------------------------------------------------------------------
136 #----------------------------------------------------------------------
138 def makeBoxInBox(win
):
139 box
= wxBoxSizer(wxVERTICAL
)
141 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
143 box2
= wxBoxSizer(wxHORIZONTAL
)
144 box2
.AddMany([ (wxButton(win
, 1010, "two"), 0, wxEXPAND
),
145 (wxButton(win
, 1010, "three"), 0, wxEXPAND
),
146 (wxButton(win
, 1010, "four"), 0, wxEXPAND
),
147 (wxButton(win
, 1010, "five"), 0, wxEXPAND
),
150 box3
= wxBoxSizer(wxVERTICAL
)
151 box3
.AddMany([ (wxButton(win
, 1010, "six"), 0, wxEXPAND
),
152 (wxButton(win
, 1010, "seven"), 2, wxEXPAND
),
153 (wxButton(win
, 1010, "eight"), 1, wxEXPAND
),
154 (wxButton(win
, 1010, "nine"), 1, wxEXPAND
),
157 box2
.Add(box3
, 1, wxEXPAND
)
158 box
.Add(box2
, 1, wxEXPAND
)
160 box
.Add(wxButton(win
, 1010, "ten"), 0, wxEXPAND
)
164 #----------------------------------------------------------------------
166 def makeBoxInBorder(win
):
167 bdr
= wxBoxSizer(wxHORIZONTAL
)
168 box
= makeSimpleBox3(win
)
169 bdr
.Add(box
, 1, wxEXPAND | wxALL
, 15)
173 #----------------------------------------------------------------------
175 def makeBorderInBox(win
):
176 insideBox
= wxBoxSizer(wxHORIZONTAL
)
178 box2
= wxBoxSizer(wxHORIZONTAL
)
179 box2
.AddMany([ (wxButton(win
, 1010, "one"), 0, wxEXPAND
),
180 (wxButton(win
, 1010, "two"), 0, wxEXPAND
),
181 (wxButton(win
, 1010, "three"), 0, wxEXPAND
),
182 (wxButton(win
, 1010, "four"), 0, wxEXPAND
),
183 (wxButton(win
, 1010, "five"), 0, wxEXPAND
),
186 insideBox
.Add(box2
, 0, wxEXPAND
)
188 bdr
= wxBoxSizer(wxHORIZONTAL
)
189 bdr
.Add(wxButton(win
, 1010, "border"), 1, wxEXPAND | wxALL
)
190 insideBox
.Add(bdr
, 1, wxEXPAND | wxALL
, 20)
192 box3
= wxBoxSizer(wxVERTICAL
)
193 box3
.AddMany([ (wxButton(win
, 1010, "six"), 0, wxEXPAND
),
194 (wxButton(win
, 1010, "seven"), 2, wxEXPAND
),
195 (wxButton(win
, 1010, "eight"), 1, wxEXPAND
),
196 (wxButton(win
, 1010, "nine"), 1, wxEXPAND
),
198 insideBox
.Add(box3
, 1, wxEXPAND
)
200 outsideBox
= wxBoxSizer(wxVERTICAL
)
201 outsideBox
.Add(wxButton(win
, 1010, "top"), 0, wxEXPAND
)
202 outsideBox
.Add(insideBox
, 1, wxEXPAND
)
203 outsideBox
.Add(wxButton(win
, 1010, "bottom"), 0, wxEXPAND
)
208 #----------------------------------------------------------------------
211 gs
= wxGridSizer(3, 3, 2, 2) # rows, cols, hgap, vgap
213 gs
.AddMany([ (wxButton(win
, 1010, 'one'), 0, wxEXPAND
),
214 (wxButton(win
, 1010, 'two'), 0, wxEXPAND
),
215 (wxButton(win
, 1010, 'three'), 0, wxEXPAND
),
216 (wxButton(win
, 1010, 'four'), 0, wxEXPAND
),
217 (wxButton(win
, 1010, 'five'), 0, wxEXPAND
),
219 (wxButton(win
, 1010, 'six'), 0, wxEXPAND
),
220 (wxButton(win
, 1010, 'seven'), 0, wxEXPAND
),
221 (wxButton(win
, 1010, 'eight'), 0, wxEXPAND
),
222 (wxButton(win
, 1010, 'nine'), 0, wxEXPAND
),
227 #----------------------------------------------------------------------
230 gs
= wxGridSizer(3, 3) # rows, cols, hgap, vgap
232 box
= wxBoxSizer(wxVERTICAL
)
233 box
.Add(wxButton(win
, 1010, 'A'), 0, wxEXPAND
)
234 box
.Add(wxButton(win
, 1010, 'B'), 1, wxEXPAND
)
236 gs2
= wxGridSizer(2,2, 4, 4)
237 gs2
.AddMany([ (wxButton(win
, 1010, 'C'), 0, wxEXPAND
),
238 (wxButton(win
, 1010, 'E'), 0, wxEXPAND
),
239 (wxButton(win
, 1010, 'F'), 0, wxEXPAND
),
240 (wxButton(win
, 1010, 'G'), 0, wxEXPAND
)])
242 gs
.AddMany([ (wxButton(win
, 1010, 'one'), 0, wxALIGN_RIGHT | wxALIGN_BOTTOM
),
243 (wxButton(win
, 1010, 'two'), 0, wxEXPAND
),
244 (wxButton(win
, 1010, 'three'), 0, wxALIGN_LEFT | wxALIGN_BOTTOM
),
245 (wxButton(win
, 1010, 'four'), 0, wxEXPAND
),
246 (wxButton(win
, 1010, 'five'), 0, wxALIGN_CENTER
),
247 (wxButton(win
, 1010, 'six'), 0, wxEXPAND
),
248 (box
, 0, wxEXPAND | wxALL
, 10),
249 (wxButton(win
, 1010, 'eight'), 0, wxEXPAND
),
250 (gs2
, 0, wxEXPAND | wxALL
, 4),
255 #----------------------------------------------------------------------
258 gs
= wxFlexGridSizer(3, 3, 2, 2) # rows, cols, hgap, vgap
260 gs
.AddMany([ (wxButton(win
, 1010, 'one'), 0, wxEXPAND
),
261 (wxButton(win
, 1010, 'two'), 0, wxEXPAND
),
262 (wxButton(win
, 1010, 'three'), 0, wxEXPAND
),
263 (wxButton(win
, 1010, 'four'), 0, wxEXPAND
),
264 #(wxButton(win, 1010, 'five'), 0, wxEXPAND),
266 (wxButton(win
, 1010, 'six'), 0, wxEXPAND
),
267 (wxButton(win
, 1010, 'seven'), 0, wxEXPAND
),
268 (wxButton(win
, 1010, 'eight'), 0, wxEXPAND
),
269 (wxButton(win
, 1010, 'nine'), 0, wxEXPAND
),
277 #----------------------------------------------------------------------
280 bpos
= wxDefaultPosition
281 bsize
= wxSize(100, 50)
282 gs
= wxGridSizer(3, 3, 2, 2) # rows, cols, hgap, vgap
284 gs
.AddMany([ (wxButton(win
, 1010, 'one', bpos
, bsize
),
285 0, wxALIGN_TOP | wxALIGN_LEFT
),
286 (wxButton(win
, 1010, 'two', bpos
, bsize
),
287 0, wxALIGN_TOP | wxALIGN_CENTER_HORIZONTAL
),
288 (wxButton(win
, 1010, 'three', bpos
, bsize
),
289 0, wxALIGN_TOP | wxALIGN_RIGHT
),
290 (wxButton(win
, 1010, 'four', bpos
, bsize
),
291 0, wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT
),
292 (wxButton(win
, 1010, 'five', bpos
, bsize
),
294 (wxButton(win
, 1010, 'six', bpos
, bsize
),
295 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT
),
296 (wxButton(win
, 1010, 'seven', bpos
, bsize
),
297 0, wxALIGN_BOTTOM | wxALIGN_LEFT
),
298 (wxButton(win
, 1010, 'eight', bpos
, bsize
),
299 0, wxALIGN_BOTTOM | wxALIGN_CENTER_HORIZONTAL
),
300 (wxButton(win
, 1010, 'nine', bpos
, bsize
),
301 0, wxALIGN_BOTTOM | wxALIGN_RIGHT
),
306 #----------------------------------------------------------------------
309 bpos
= wxDefaultPosition
310 bsize
= wxSize(100, 50)
311 gs
= wxGridSizer(3, 3, 2, 2) # rows, cols, hgap, vgap
313 gs
.AddMany([ (wxButton(win
, 1010, 'one', bpos
, bsize
),
314 0, wxSHAPED | wxALIGN_TOP | wxALIGN_LEFT
),
315 (wxButton(win
, 1010, 'two', bpos
, bsize
),
316 0, wxSHAPED | wxALIGN_TOP | wxALIGN_CENTER_HORIZONTAL
),
317 (wxButton(win
, 1010, 'three', bpos
, bsize
),
318 0, wxSHAPED | wxALIGN_TOP | wxALIGN_RIGHT
),
319 (wxButton(win
, 1010, 'four', bpos
, bsize
),
320 0, wxSHAPED | wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT
),
321 (wxButton(win
, 1010, 'five', bpos
, bsize
),
322 0, wxSHAPED | wxALIGN_CENTER
),
323 (wxButton(win
, 1010, 'six', bpos
, bsize
),
324 0, wxSHAPED | wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT
),
325 (wxButton(win
, 1010, 'seven', bpos
, bsize
),
326 0, wxSHAPED | wxALIGN_BOTTOM | wxALIGN_LEFT
),
327 (wxButton(win
, 1010, 'eight', bpos
, bsize
),
328 0, wxSHAPED | wxALIGN_BOTTOM | wxALIGN_CENTER_HORIZONTAL
),
329 (wxButton(win
, 1010, 'nine', bpos
, bsize
),
330 0, wxSHAPED | wxALIGN_BOTTOM | wxALIGN_RIGHT
),
335 #----------------------------------------------------------------------
337 def makeSimpleBoxShaped(win
):
338 box
= wxBoxSizer(wxHORIZONTAL
)
339 box
.Add(wxButton(win
, 1010, "one"), 0, wxEXPAND
)
340 box
.Add(wxButton(win
, 1010, "two"), 0, wxEXPAND
)
341 box
.Add(wxButton(win
, 1010, "three"), 0, wxEXPAND
)
342 box
.Add(wxButton(win
, 1010, "four"), 0, wxEXPAND
)
343 box
.Add(wxButton(win
, 1010, "five"), 1, wxSHAPED
)
347 #----------------------------------------------------------------------
350 ("Simple horizontal boxes", makeSimpleBox1
,
351 "This is a HORIZONTAL box sizer with four non-stretchable buttons held "
352 "within it. Notice that the buttons are added and aligned in the horizontal "
353 "dimension. Also notice that they are fixed size in the horizontal dimension, "
354 "but will stretch vertically."
357 ("Simple vertical boxes", makeSimpleBox2
,
358 "Exactly the same as the previous sample but using a VERTICAL box sizer "
359 "instead of a HORIZONTAL one."
362 ("Add a stretchable", makeSimpleBox3
,
363 "We've added one more button with the strechable flag turned on. Notice "
364 "how it grows to fill the extra space in the otherwise fixed dimension."
367 ("More than one stretchable", makeSimpleBox4
,
368 "Here there are several items that are stretchable, they all divide up the "
369 "extra space evenly."
372 ("Weighting factor", makeSimpleBox5
,
373 "This one shows more than one strechable, but one of them has a weighting "
374 "factor so it gets more of the free space."
377 ("Edge Affinity", makeSimpleBox6
,
378 "For items that don't completly fill their allotted space, and don't "
379 "stretch, you can specify which side (or the center) they should stay "
383 ("Spacer", makeSimpleBox7
,
384 "You can add empty space to be managed by a Sizer just as if it were a "
385 "window or another Sizer."
388 ("Centering in available space", makeSimpleBox8
,
389 "This one shows an item that does not expand to fill it's space, but rather"
390 "stays centered within it."
393 # ("Percent Sizer", makeSimpleBox6,
394 # "You can use the wxBoxSizer like a Percent Sizer. Just make sure that all "
395 # "the weighting factors add up to 100!"
400 ("Simple border sizer", makeSimpleBorder1
,
401 "The wxBoxSizer can leave empty space around its contents. This one "
402 "gives a border all the way around."
405 ("East and West border", makeSimpleBorder2
,
406 "You can pick and choose which sides have borders."
409 ("North and West border", makeSimpleBorder3
,
410 "You can pick and choose which sides have borders."
415 ("Boxes inside of boxes", makeBoxInBox
,
416 "This one shows nesting of boxes within boxes within boxes, using both "
417 "orientations. Notice also that button seven has a greater weighting "
418 "factor than its siblings."
421 ("Boxes inside a Border", makeBoxInBorder
,
422 "Sizers of different types can be nested withing each other as well. "
423 "Here is a box sizer with several buttons embedded within a border sizer."
426 ("Border in a Box", makeBorderInBox
,
427 "Another nesting example. This one has Boxes and a Border inside another Box."
432 ("Simple Grid", makeGrid1
,
433 "This is an example of the wxGridSizer. In this case all row heights "
434 "and column widths are kept the same as all the others and all items "
435 "fill their available space. The horzontal and vertical gaps are set to "
439 ("More Grid Features", makeGrid2
,
440 "This is another example of the wxGridSizer. This one has no gaps in the grid, "
441 "but various cells are given different alignment options and some of them "
442 "hold nested sizers."
445 ("Flexible Grid", makeGrid3
,
446 "This grid allows the rows to have different heights and the columns to have "
447 "different widths. You can also specify rows and columns that are growable, "
448 "which we have done for the first and last row and the middle column for "
450 "\nThere is also a spacer in the middle cell instead of an actual window."
453 ("Grid with Alignment", makeGrid4
,
454 "New alignment flags allow for the positioning of items in any corner or centered "
460 ("Proportional resize", makeSimpleBoxShaped
,
461 "Managed items can preserve their original aspect ratio. The last item has the "
462 "wxSHAPED flag set and will resize proportional to its origingal size."
465 ("Proportional resize with Alignments", makeShapes
,
466 "This one shows various alignments as well as proportional resizing for all items."
470 #----------------------------------------------------------------------
472 class TestFrame(wxFrame
):
473 def __init__(self
, parent
, title
, sizerFunc
):
474 wxFrame
.__init
__(self
, parent
, -1, title
)
475 EVT_BUTTON(self
, 1010, self
.OnButton
)
477 self
.sizer
= sizerFunc(self
)
478 self
.CreateStatusBar()
479 self
.SetStatusText("Resize this frame to see how the sizers respond...")
482 self
.SetAutoLayout(true
)
483 self
.SetSizer(self
.sizer
)
485 def OnCloseWindow(self
, event
):
486 self
.MakeModal(false
)
489 def OnButton(self
, event
):
492 #----------------------------------------------------------------------
496 class TestSelectionPanel(wxPanel
):
497 def __init__(self
, parent
, frame
):
498 wxPanel
.__init
__(self
, parent
, -1)
501 self
.list = wxListBox(self
, 401,
502 wxDLG_PNT(self
, 10, 10), wxDLG_SZE(self
, 100, 100),
504 EVT_LISTBOX(self
, 401, self
.OnSelect
)
505 EVT_LISTBOX_DCLICK(self
, 401, self
.OnDClick
)
507 self
.btn
= wxButton(self
, 402, "Try it!", wxDLG_PNT(self
, 120, 10)).SetDefault()
508 EVT_BUTTON(self
, 402, self
.OnDClick
)
510 self
.text
= wxTextCtrl(self
, -1, "",
511 wxDLG_PNT(self
, 10, 115),
512 wxDLG_SZE(self
, 200, 50),
513 wxTE_MULTILINE | wxTE_READONLY
)
515 for item
in theTests
:
516 self
.list.Append(item
[0])
520 def OnSelect(self
, event
):
521 pos
= self
.list.GetSelection()
522 self
.text
.SetValue(theTests
[pos
][2])
525 def OnDClick(self
, event
):
526 pos
= self
.list.GetSelection()
527 title
= theTests
[pos
][0]
528 func
= theTests
[pos
][1]
531 win
= TestFrame(self
, title
, func
)
532 win
.CentreOnParent(wxBOTH
)
536 #----------------------------------------------------------------------
538 def runTest(frame
, nb
, log
):
539 win
= TestSelectionPanel(nb
, frame
)
543 #wxSizer.__doc__ + '\n' + '-' * 80 + '\n' + \
544 #wxBoxSizer.__doc__ + '\n' + '-' * 80 + '\n' + \
545 #wxBorderSizer.__doc__
547 #----------------------------------------------------------------------
551 if __name__
== '__main__':
553 class MainFrame(wxFrame
):
555 wxFrame
.__init
__(self
, NULL
, -1, "Testing...")
557 self
.CreateStatusBar()
558 mainmenu
= wxMenuBar()
560 menu
.Append(200, 'E&xit', 'Get the heck outta here!')
561 mainmenu
.Append(menu
, "&File")
562 self
.SetMenuBar(mainmenu
)
563 EVT_MENU(self
, 200, self
.OnExit
)
564 self
.panel
= TestSelectionPanel(self
, self
)
565 self
.SetSize(wxSize(400, 380))
567 def OnCloseWindow(self
, event
):
570 def OnExit(self
, event
):
574 class TestApp(wxApp
):
578 self
.SetTopWindow(frame
)
585 #----------------------------------------------------------------------