How to use sizers - Part 1 (Phoenix)
Keywords : Sizers.
Demonstrating :
Tested py3.x, wx4.x and Win10.
Are you ready to use some samples ?
Test, modify, correct, complete, improve and share your discoveries !
1 # -*- coding: iso-8859-1 -*-
2
3 # Name : LearnSizers1.py
4 # Purpose : A demo app to learn sizers
5 # Author : Jean-Michel Fauth, Switzerland
6 # Copyright : (c) 2005 Jean-Michel Fauth
7 # Licence : GPL
8
9 # os dev : windows 98/10/11
10 # py dev : Python 2.4.1/Python 3.11.6
11 # wx dev : wxPython 2.6.1/wxPython 4.2.2
12 # Revision : 27 June 2005
13
14 # Contributor and minor fixes : Marcelo Barbero, Argentina
15 # Revision : 03 August 2005 (Marcelo Barbero)
16 # Revision : 25 November 2023 (Ecco)
17
18 # LearnSizer*.py is a small application aimed to learn the sizers and
19 # their usage.
20 # This application is a collection of panels. Each panel contains
21 # a few widgets laid out with sizers. The panels are numbered
22 # MyPanel_0 to MyPanel_n. The panel 0 is the simpliest case. The
23 # order of the panels follows an increasing complexity in the
24 # layout. It is warmly recommended to start with MyPanel_0.
25 #
26 # Do not forget :
27 # - the wxPython doc
28 # - the wiki page http://wiki.wxpython.org/index.cgi/UsingSizers
29 # - dir(<instance of a sizer>) for a list of methods
30 # - print sizer.method.__doc__
31
32
33 import os
34 import sys
35 import wx
36
37 class ColWin(wx.Window):
38 """- A wx.Window with a coloured background
39 - pos and size == (-1, -1) since sizers are used"""
40
41 def __init__(self, parent, id, BackColour):
42 wx.Window.__init__(self, parent, id, (-1, -1), (-1, -1), wx.SIMPLE_BORDER)
43 self.SetBackgroundColour(BackColour)
44
45
46 class MyPanel_0(wx.Panel):
47 """- The simpliest sizer, a ColWin
48 - use of named arguments"""
49
50 def __init__(self, parent):
51 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
52
53 wgreen = ColWin(self, wx.NewIdRef(), wx.GREEN)
54
55 b = 20
56 vsizer1 = wx.BoxSizer(orient=wx.VERTICAL)
57 # or
58 # vsizer1 = wx.BoxSizer(orient=wx.HORIZONTAL)
59 vsizer1.Add(window=wgreen, proportion=1, flag=wx.EXPAND | wx.ALL, border=b)
60 self.SetSizer(vsizer1)
61
62
63 class MyPanel_1(wx.Panel):
64 """- Two ColWins, vertically"""
65
66 def __init__(self, parent):
67 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
68
69 wred = ColWin(self, wx.NewIdRef(), wx.RED)
70 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
71
72 b = 5
73 vsizer1 = wx.BoxSizer(wx.VERTICAL)
74 vsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
75 vsizer1.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
76 self.SetSizer(vsizer1)
77
78
79 class MyPanel_2(wx.Panel):
80 """- Three ColWins, horizontally """
81
82 def __init__(self, parent):
83 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
84
85 wred = ColWin(self, wx.NewIdRef(), wx.RED)
86 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
87 wgreen = ColWin(self, wx.NewIdRef(), wx.GREEN)
88
89 b = 5
90 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
91 hsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
92 hsizer1.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
93 hsizer1.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
94 self.SetSizer(hsizer1)
95
96
97 class MyPanel_3(wx.Panel):
98 """- Three ColWins, horizontally, height ratio 1:2:3"""
99
100 def __init__(self, parent):
101 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
102
103 wred = ColWin(self, wx.NewIdRef(), wx.RED)
104 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
105 wgreen = ColWin(self, wx.NewIdRef(), wx.GREEN)
106
107 b = 5
108 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
109 hsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
110 hsizer1.Add(wblue, 2, wx.EXPAND | wx.ALL, b)
111 hsizer1.Add(wgreen, 3, wx.EXPAND | wx.ALL, b)
112 self.SetSizer(hsizer1)
113
114
115 class MyPanel_4(wx.Panel):
116 """- Two ColWins, vertically, a fixed width of 50 pixels between
117 the two items"""
118
119 def __init__(self, parent):
120 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
121
122 wred = ColWin(self, wx.NewIdRef(), wx.RED)
123 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
124
125 b = 5
126 vsizer1 = wx.BoxSizer(wx.VERTICAL)
127 vsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
128 vsizer1.Add((-1, 50), 0, wx.ALL, b)
129 vsizer1.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
130 self.SetSizer(vsizer1)
131
132
133 class MyPanel_5(wx.Panel):
134 """- Two items: a ColWin and a Button, vertically
135 - the Button is either left/right aligned or centered
136 - comment / uncomment for testing the Button alignment
137 - wx.ALIGN_LEFT is the default value"""
138
139 def __init__(self, parent):
140 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
141
142 wred = ColWin(self, wx.NewIdRef(), wx.RED)
143 b1 = wx.Button(self, wx.NewIdRef(), 'button1', (-1, -1), wx.DefaultSize)
144
145 b = 5
146 vsizer1 = wx.BoxSizer(wx.VERTICAL)
147 vsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
148
149 #~ vsizer1.Add(b1, 0, wx.ALIGN_LEFT | wx.ALL, b)
150 #~ vsizer1.Add(b1, 0, wx.ALIGN_RIGHT | wx.ALL, b)
151 vsizer1.Add(b1, 0, wx.ALIGN_CENTER | wx.ALL, b)
152 self.SetSizer(vsizer1)
153
154
155 class MyPanel_6(wx.Panel):
156 """- Three items: a ColWin and two Buttons
157 - the Buttons are either left/right aligned or centered"""
158
159 def __init__(self, parent):
160 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
161
162 wred = ColWin(self, wx.NewIdRef(), wx.RED)
163 b1 = wx.Button(self, wx.NewIdRef(), '&OK', (-1, -1), wx.DefaultSize)
164 b2 = wx.Button(self, wx.NewIdRef(), '&Cancel', (-1, -1), wx.DefaultSize)
165
166 b = 10
167 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
168 hsizer1.Add(b1, 0)
169 hsizer1.Add(b2, 0, wx.LEFT, b)
170
171 b = 5
172 vsizer1 = wx.BoxSizer(wx.VERTICAL)
173 vsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
174 vsizer1.Add(hsizer1, 0, wx.ALIGN_RIGHT | wx.ALL, b)
175 #~ vsizer1.Add(hsizer1, 0, wx.ALIGN_CENTER | wx.ALL, b)
176 #~ vsizer1.Add(hsizer1, 0, wx.ALIGN_LEFT | wx.ALL, b)
177 self.SetSizer(vsizer1)
178
179
180 class MyPanel_7(wx.Panel):
181 """- Four items: a ColWin, two Buttons, and a StaticLine
182 - the Buttons are either left/right aligned or centered
183 - the height of the static line == 2, the wx.GROW flag specifies an
184 horizontal expansion, since the StaticLine is in a vertical sizer"""
185
186 def __init__(self, parent):
187 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
188
189 wred = ColWin(self, wx.NewIdRef(), wx.RED)
190 b1 = wx.Button(self, wx.NewIdRef(), '&OK', (-1, -1), wx.DefaultSize)
191 b2 = wx.Button(self, wx.NewIdRef(), '&Cancel', (-1, -1), wx.DefaultSize)
192 staline = wx.StaticLine(self, wx.NewIdRef(), (-1, -1), (-1, 2), wx.LI_HORIZONTAL)
193
194 b = 5
195 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
196 hsizer1.Add(b1, 0)
197 hsizer1.Add(b2, 0, wx.LEFT, b)
198
199 vsizer1 = wx.BoxSizer(wx.VERTICAL)
200 vsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
201 vsizer1.Add(staline, 0, wx.GROW | wx.ALL, b)
202 vsizer1.Add(hsizer1, 0, wx.ALIGN_RIGHT | wx.ALL, b)
203 self.SetSizer(vsizer1)
204
205
206 class MyPanel_8(wx.Panel):
207 """- 7 items: 2 ColWins and 5 Buttons
208 - ColWins: one is sizable, the other not
209 - Buttons at the right of the frame
210 - something like a toolbar at the right"""
211
212 def __init__(self, parent):
213 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
214
215 wred = ColWin(self, wx.NewIdRef(), wx.RED)
216 wwhite = ColWin(self, wx.NewIdRef(), wx.WHITE)
217
218 b1 = wx.Button(self, wx.NewIdRef(), '1', (-1, -1), wx.DefaultSize)
219 b2 = wx.Button(self, wx.NewIdRef(), '2', (-1, -1), wx.DefaultSize)
220 b3 = wx.Button(self, wx.NewIdRef(), '3', (-1, -1), wx.DefaultSize)
221 b4 = wx.Button(self, wx.NewIdRef(), '4', (-1, -1), wx.DefaultSize)
222 b5 = wx.Button(self, wx.NewIdRef(), '5', (-1, -1), wx.DefaultSize)
223
224 vsizer1 = wx.BoxSizer(wx.VERTICAL)
225 b = 3
226 vsizer1.Add(wred, 1, wx.EXPAND | wx.BOTTOM, b)
227 vsizer1.Add(wwhite, 0, wx.EXPAND, b)
228
229 vsizer2 = wx.BoxSizer(wx.VERTICAL)
230 b = 5
231 vsizer2.Add(b1, 0, wx.BOTTOM, b)
232 vsizer2.Add(b2, 0, wx.BOTTOM, b)
233 vsizer2.Add(b3, 0, wx.BOTTOM, b)
234 vsizer2.Add(b4, 0, wx.BOTTOM, b)
235 vsizer2.Add(b5, 0, wx.BOTTOM, b)
236
237 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
238 b = 10
239 hsizer1.Add(vsizer1, 1, wx.EXPAND | wx.LEFT | wx.TOP | wx.BOTTOM, b)
240 hsizer1.Add(vsizer2, 0, wx.EXPAND| wx.ALL, b)
241
242 self.SetSizer(hsizer1)
243
244
245 class MyPanel_9(wx.Panel):
246 """- 7 items: 2 ColWins and 5 Buttons
247 - the ColWins are sizable
248 - the Buttons have a fixed height and a default width
249 - something like a toolbar at the top"""
250
251 def __init__(self, parent):
252 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
253
254 wred = ColWin(self, wx.NewIdRef(), wx.RED)
255 wwhite = ColWin(self, wx.NewIdRef(), wx.WHITE)
256
257 h = 40
258 b1 = wx.Button(self, wx.NewIdRef(), '1', (-1, -1), (-1, h))
259 b2 = wx.Button(self, wx.NewIdRef(), '2', (-1, -1), (-1, h))
260 b3 = wx.Button(self, wx.NewIdRef(), '3', (-1, -1), (-1, h))
261 b4 = wx.Button(self, wx.NewIdRef(), '4', (-1, -1), (-1, h))
262 b5 = wx.Button(self, wx.NewIdRef(), '5', (-1, -1), (-1, h))
263
264 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
265 hsizer1.Add(b1, 0)
266 hsizer1.Add(b2, 0)
267 hsizer1.Add(b3, 0)
268 hsizer1.Add(b4, 0)
269 hsizer1.Add(b5, 0)
270
271 b = 2
272 hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
273 hsizer2.Add(wred, 1, wx.EXPAND | wx.RIGHT, b)
274 hsizer2.Add(wwhite, 1, wx.EXPAND | wx.LEFT, b)
275
276 b = 5
277 vsizer1 = wx.BoxSizer(wx.VERTICAL)
278 vsizer1.Add(hsizer1, 0, wx.EXPAND, b)
279 vsizer1.Add(hsizer2, 1, wx.ALL | wx.EXPAND, b)
280
281 self.SetSizer(vsizer1)
282
283
284 class MyPanel_10(wx.Panel):
285 """- Three ColWins in a StaticBoxSizer, vertically"""
286
287 def __init__(self, parent):
288 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
289
290 wred = ColWin(self, wx.NewIdRef(), wx.RED)
291 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
292 wgreen = ColWin(self, wx.NewIdRef(), wx.GREEN)
293
294 b = 10 #inside the staticbox
295 vsbsizer1 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), 'StaticboxSizer with a caption'), wx.VERTICAL)
296 vsbsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
297 vsbsizer1.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
298 vsbsizer1.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
299 self.SetSizer(vsbsizer1)
300
301
302 class MyPanel_11(wx.Panel):
303 """- Three ColWins in a StaticBoxSizer, vertically
304 - the StaticBoxSizer is in a sizer to enable a border"""
305
306 def __init__(self, parent):
307 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
308
309 wred = ColWin(self, wx.NewIdRef(), wx.RED)
310 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
311 wgreen = ColWin(self, wx.NewIdRef(), wx.GREEN)
312
313 b = 10 #inside the staticbox
314 vsbsizer1 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), 'StaticboxSizer with a caption'), wx.VERTICAL)
315 vsbsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
316 vsbsizer1.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
317 vsbsizer1.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
318
319 b = 20
320 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
321 hsizer1.Add(vsbsizer1, 1, wx.EXPAND | wx.ALL, b)
322
323 self.SetSizer(hsizer1)
324
325
326 class MyPanel_12(wx.Panel):
327 """- 5 ColWins, vertically
328 - ColWins 2 and 4 are in StaticBoxSizer"""
329
330 def __init__(self, parent):
331 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
332
333 wred = ColWin(self, wx.NewIdRef(), wx.RED)
334 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
335 wgreen = ColWin(self, wx.NewIdRef(), wx.GREEN)
336 wwhite = ColWin(self, wx.NewIdRef(), wx.WHITE)
337 wyellow = ColWin(self, wx.NewIdRef(), wx.Colour('yellow'))
338
339 b = 10
340 vsbsizer1 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), 'blue'), wx.VERTICAL)
341 vsbsizer1.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
342
343 vsbsizer2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), 'white'), wx.VERTICAL)
344 vsbsizer2.Add(wwhite, 1, wx.EXPAND | wx.ALL, b)
345
346 b = 5
347 vsizer1 = wx.BoxSizer(wx.VERTICAL)
348 vsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
349 vsizer1.Add(vsbsizer1, 1, wx.EXPAND | wx.ALL, b)
350 vsizer1.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
351 vsizer1.Add(vsbsizer2, 1, wx.EXPAND | wx.ALL, b)
352 vsizer1.Add(wyellow, 1, wx.EXPAND | wx.ALL, b)
353
354 self.SetSizer(vsizer1)
355
356
357 class MyPanel_13(wx.Panel):
358 """- 3 StaticBoxSizers with a ColWin in each
359 - the StaticBoxSizers are arranged vertically"""
360
361 def __init__(self, parent):
362 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
363
364 wred = ColWin(self, wx.NewIdRef(), wx.Colour('red'))
365 wgreen = ColWin(self, wx.NewIdRef(), wx.Colour('green'))
366 wblue = ColWin(self, wx.NewIdRef(), wx.Colour('blue'))
367
368 b = 10
369 sizer1 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), 'Staticbox'), wx.VERTICAL)
370 sizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
371
372 sizer2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), 'Staticbox'), wx.VERTICAL)
373 sizer2.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
374
375 sizer4 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), 'Staticbox'), wx.VERTICAL)
376 sizer4.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
377
378 #tricky: the 2nd and 3rd staticbox sizer are shifted one pixel to the
379 #left, try with b2 = 0
380 b1, b2 = 0, -1
381 sizer3 = wx.BoxSizer(wx.HORIZONTAL)
382 sizer3.Add(sizer1, 1, wx.EXPAND, b1)
383 sizer3.Add(sizer2, 1, wx.EXPAND | wx.LEFT, b2)
384 sizer3.Add(sizer4, 1, wx.EXPAND | wx.LEFT, b2)
385
386 self.SetSizer(sizer3)
387
388
389 class MyPanel_14(wx.Panel):
390 """- 3 ColWins, horizontally, 2 spacers with a fixed height"""
391
392 def __init__(self, parent):
393 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
394
395 wred = ColWin(self, wx.NewIdRef(), wx.RED)
396 wblue = ColWin(self, wx.NewIdRef(), wx.BLUE)
397 wgreen = ColWin(self, wx.NewIdRef(), wx.GREEN)
398
399 b = 0
400 h = 20
401 hsizer1 = wx.BoxSizer(wx.VERTICAL)
402 hsizer1.Add(wred, 1, wx.EXPAND | wx.ALL, b)
403 hsizer1.Add((-1, h))
404 hsizer1.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
405 hsizer1.Add((-1, h))
406 hsizer1.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
407 self.SetSizer(hsizer1)
408
409
410 class MyPanel_15(wx.Panel):
411 """- 4 items: a ColWin and 3 Buttons
412 - Buttons 1 and 3 are left/right aligned, Button 2 is centered
413 - use of spacers to set a gap between the Buttons.
414 - better way?"""
415
416 def __init__(self, parent):
417 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
418
419 wgreen = ColWin(self, wx.NewIdRef(), wx.Colour('green'))
420 b1 = wx.Button(self, wx.NewIdRef(), 'button1', (-1, -1), wx.DefaultSize)
421 b2 = wx.Button(self, wx.NewIdRef(), 'button2', (-1, -1), wx.DefaultSize)
422 b3 = wx.Button(self, wx.NewIdRef(), 'button3', (-1, -1), wx.DefaultSize)
423
424 b = 0
425 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
426 hsizer1.Add(b1, 0, wx.ALL, b)
427 hsizer1.Add((-1, -1), 1)
428 hsizer1.Add(b2, 0, wx.ALL, b)
429 hsizer1.Add((-1, -1), 1)
430 hsizer1.Add(b3, 0, wx.ALL, b)
431
432 #tip: this does not work
433 #~ b = 0
434 #~ hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
435 #~ hsizer1.Add(b1, 0, wx.ALIGN_LEFT, b)
436 #~ hsizer1.Add(b2, 0, wx.ALIGN_CENTER, b)
437 #~ hsizer1.Add(b3, 0, wx.ALIGN_RIGHT, b)
438
439 b = 5
440 vsizer2 = wx.BoxSizer(wx.VERTICAL)
441 vsizer2.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
442 vsizer2.Add(hsizer1, 0, wx.EXPAND | wx.ALL, b)
443 self.SetSizer(vsizer2)
444
445
446 class MyPanel_16(wx.Panel):
447 """- 4 items: a ColWin, 3 Buttons
448 - Buttons 1 and 3 are top/bottom aligned, Button 2 is centered
449 - use of spacers to set a gap between the Buttons
450 - better way?"""
451
452 def __init__(self, parent):
453 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
454
455 wgreen = ColWin(self, wx.NewIdRef(), wx.Colour('green'))
456 b1 = wx.Button(self, wx.NewIdRef(), 'button1', (-1, -1), wx.DefaultSize)
457 b2 = wx.Button(self, wx.NewIdRef(), 'button2', (-1, -1), wx.DefaultSize)
458 b3 = wx.Button(self, wx.NewIdRef(), 'button3', (-1, -1), wx.DefaultSize)
459
460 b = 0
461 vsizer1 = wx.BoxSizer(wx.VERTICAL)
462 vsizer1.Add(b1, 0, wx.ALL, b)
463 vsizer1.Add((-1, -1), 1)
464 vsizer1.Add(b2, 0, wx.ALL, b)
465 vsizer1.Add((-1, -1), 1)
466 vsizer1.Add(b3, 0, wx.ALL, b)
467
468 b = 5
469 hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
470 hsizer2.Add(vsizer1, 0, wx.EXPAND | wx.ALL, b)
471 hsizer2.Add(wgreen, 1, wx.EXPAND | wx.ALL, b)
472 self.SetSizer(hsizer2)
473
474
475 class MyPanel_17(wx.Panel):
476 """- An input box"""
477 def __init__(self, parent):
478 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
479 self.parent = parent
480
481 s = 'abc' + os.linesep + 'def' + os.linesep + 'ghi'
482 statxt = wx.StaticText(self, -1, s, (-1, -1), (-1, -1))
483 txt = wx.TextCtrl(self, wx.NewIdRef(), 'input', (-1, -1), (-1, -1))
484 staline = wx.StaticLine(self, -1, (-1, -1), (-1, -1), wx.LI_HORIZONTAL)
485
486 b1 = wx.Button(self, wx.NewIdRef(), "&OK", (-1, -1), wx.DefaultSize)
487 b2 = wx.Button(self, wx.NewIdRef(), "&Cancel", (-1, -1), wx.DefaultSize)
488
489 b = 10
490 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
491 hsizer1.Add(b1, 0)
492 hsizer1.Add(b2, 0, wx.LEFT, b)
493
494 b = 5
495 vsizer1 = wx.BoxSizer(wx.VERTICAL)
496 vsizer1.Add(statxt, 0, wx.GROW | wx.ALL, b)
497 vsizer1.Add(txt, 0, wx.GROW | wx.ALL, b)
498 vsizer1.Add(staline, 0, wx.GROW | wx.ALL, b)
499 vsizer1.Add(hsizer1, 0, wx.ALIGN_RIGHT | wx.ALL, b)
500 vsizer1.SetMinSize((300, -1))
501 self.SetSizerAndFit(vsizer1)
502
503 #the items are not sizable, once set, pass the fitted size to
504 #the parent
505 self.parent.SetClientSize(vsizer1.GetSize())
506
507
508 class MyPanel_18(wx.Panel):
509 """- A message box"""
510
511 def __init__(self, parent):
512 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
513 self.parent = parent
514
515 s = 'This is a rather long message with.'
516 s += (os.linesep + 'a lot of lines...')*20
517
518 statxt = wx.StaticText(self, -1, s, (-1, -1), (-1, -1))
519 staline = wx.StaticLine(self, -1, (-1, -1), (-1, -1), wx.LI_HORIZONTAL)
520 b1 = wx.Button(self, wx.NewIdRef(), "&OK", (-1, -1), wx.DefaultSize)
521
522 b = 5
523 vsizer1 = wx.BoxSizer(wx.VERTICAL)
524 vsizer1.Add(statxt, 1, wx.EXPAND | wx.ALL, b)
525 vsizer1.Add(staline, 0, wx.GROW | wx.ALL, b)
526 vsizer1.Add(b1, 0, wx.ALIGN_CENTER | wx.ALL, b)
527 vsizer1.SetMinSize((200, -1))
528 self.SetSizerAndFit(vsizer1)
529
530 self.parent.SetClientSize(vsizer1.GetSize())
531
532
533 class MyPanel_19(wx.Panel):
534 """- A serie of couples, StaticText-TextCtrl
535 - Buttons ok and cancel"""
536
537 def __init__(self, parent):
538 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
539 self.parent = parent
540
541 lab1 = wx.StaticText(self, -1, 'hydrogen :', (-1, -1), (-1, -1), wx.ALIGN_RIGHT)
542 lab2 = wx.StaticText(self, -1, 'tin :', (-1, -1), (-1, -1), wx.ALIGN_RIGHT)
543 lab3 = wx.StaticText(self, -1, 'mendelevium :', (-1, -1), (-1, -1), wx.ALIGN_RIGHT)
544 lab4 = wx.StaticText(self, -1, 'carbon :', (-1, -1), (-1, -1), wx.ALIGN_RIGHT)
545 txt1 = wx.TextCtrl(self, -1, '', (-1, -1), (-1, -1))
546 txt2 = wx.TextCtrl(self, -1, '', (-1, -1), (-1, -1))
547 txt3 = wx.TextCtrl(self, -1, '', (-1, -1), (-1, -1))
548 txt4 = wx.TextCtrl(self, -1, '', (-1, -1), (-1, -1))
549 b1 = wx.Button(self, wx.NewIdRef(), '&OK', (-1, -1), wx.DefaultSize)
550 b2 = wx.Button(self, wx.NewIdRef(), '&Cancel', (-1, -1), wx.DefaultSize)
551 staline = wx.StaticLine(self, wx.NewIdRef(), (-1, -1), (-1, 2), wx.LI_HORIZONTAL)
552
553 b = 5
554 w = 100
555 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
556 hsizer1.Add(lab1, 0, wx.RIGHT, b)
557 hsizer1.Add(txt1, 1, wx.GROW, b)
558 hsizer1.SetItemMinSize(lab1, (w, -1))
559
560 hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
561 hsizer2.Add(lab2, 0, wx.RIGHT, b)
562 hsizer2.Add(txt2, 1, wx.GROW, b)
563 hsizer2.SetItemMinSize(lab2, (w, -1))
564
565 hsizer3 = wx.BoxSizer(wx.HORIZONTAL)
566 hsizer3.Add(lab3, 0, wx.RIGHT, b)
567 hsizer3.Add(txt3, 1, wx.GROW, b)
568 hsizer3.SetItemMinSize(lab3, (w, -1))
569
570 hsizer4 = wx.BoxSizer(wx.HORIZONTAL)
571 hsizer4.Add(lab4, 0, wx.RIGHT, b)
572 hsizer4.Add(txt4, 1, wx.GROW, b)
573 hsizer4.SetItemMinSize(lab4, (w, -1))
574
575 hsizer5 = wx.BoxSizer(wx.HORIZONTAL)
576 hsizer5.Add(b1, 0)
577 hsizer5.Add(b2, 0, wx.LEFT, 10)
578
579 b = 5
580 vsizer1 = wx.BoxSizer(wx.VERTICAL)
581 vsizer1.Add(hsizer1, 0, wx.EXPAND | wx.ALL, b)
582 vsizer1.Add(hsizer2, 0, wx.EXPAND | wx.ALL, b)
583 vsizer1.Add(hsizer3, 0, wx.EXPAND | wx.ALL, b)
584 vsizer1.Add(hsizer4, 0, wx.EXPAND | wx.ALL, b)
585 vsizer1.Add(staline, 0, wx.GROW | wx.ALL, b)
586 vsizer1.Add(hsizer5, 0, wx.ALIGN_RIGHT | wx.ALL, b)
587
588 self.SetSizerAndFit(vsizer1)
589 self.parent.SetClientSize(vsizer1.GetSize())
590
591
592 class MyPanel_20(wx.Panel):
593 """- A FlexGridSizer
594 - 4 ColWins as cells, all sizeable"""
595
596 def __init__(self, parent):
597 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
598
599 wred = ColWin(self, -1, wx.RED)
600 wblue = ColWin(self, -1, wx.BLUE)
601 wwhite = ColWin(self, -1, wx.WHITE)
602 wcyan = ColWin(self, -1, wx.CYAN)
603
604 hgap, vgap = 0, 0
605 nrows, ncols = 2, 2
606 fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)
607
608 b = 5
609 fgs.AddMany([(wred, 1, wx.EXPAND | wx.ALL, b),
610 (wblue, 1, wx.EXPAND | wx.ALL, b),
611 (wwhite, 1, wx.EXPAND | wx.ALL, b),
612 (wcyan, 1, wx.EXPAND | wx.ALL, b),
613 ])
614
615 # or
616 #~ fgs.Add(wred, 1, wx.EXPAND | wx.ALL, b)
617 #~ fgs.Add(wblue, 1, wx.EXPAND | wx.ALL, b)
618 #~ fgs.Add(wwhite, 1, wx.EXPAND | wx.ALL, b)
619 #~ fgs.Add(wcyan, 1, wx.EXPAND | wx.ALL, b)
620
621 #set all rows and cols sizable, try to comment / uncomment
622 fgs.AddGrowableRow(0)
623 fgs.AddGrowableRow(1)
624 fgs.AddGrowableCol(0)
625 fgs.AddGrowableCol(1)
626
627 self.SetSizer(fgs)
628
629
630 class MyPanel_21(wx.Panel):
631 """- A FlexgridSizer with 4 cells
632 - cell (0, 0) is a column of 3 Buttons
633 - cell (1, 1) is a row of 3 Buttons
634 - cell (1, 0) is empty
635 - cell (0, 1) is a ColWin"""
636
637 def __init__(self, parent):
638 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
639
640 wwhite = ColWin(self, -1, wx.WHITE)
641 b1 = wx.Button(self, -1, 'button1', (-1, -1), wx.DefaultSize)
642 b2 = wx.Button(self, -1, 'button2', (-1, -1), wx.DefaultSize)
643 b3 = wx.Button(self, -1, 'button3', (-1, -1), wx.DefaultSize)
644 b4 = wx.Button(self, -1, 'button4', (-1, -1), wx.DefaultSize)
645 b5 = wx.Button(self, -1, 'button5', (-1, -1), wx.DefaultSize)
646 b6 = wx.Button(self, -1, 'button6', (-1, -1), wx.DefaultSize)
647
648 b = 0
649 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
650 hsizer1.Add(b1, 0, wx.ALL, b)
651 hsizer1.Add((-1, -1), 1)
652 hsizer1.Add(b2, 0, wx.ALL, b)
653 hsizer1.Add((-1, -1), 1)
654 hsizer1.Add(b3, 0, wx.ALL, b)
655
656 b = 0
657 vsizer1 = wx.BoxSizer(wx.VERTICAL)
658 vsizer1.Add(b4, 0, wx.ALL, b)
659 vsizer1.Add((-1, -1), 1)
660 vsizer1.Add(b5, 0, wx.ALL, b)
661 vsizer1.Add((-1, -1), 1)
662 vsizer1.Add(b6, 0, wx.ALL, b)
663
664 hgap, vgap = 0, 0
665 nrows, ncols = 2, 2
666 fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)
667
668 b = 5
669 fgs.AddMany([(vsizer1, 1, wx.EXPAND | wx.ALL, b),
670 (wwhite, 1, wx.EXPAND | wx.ALL, b),
671 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
672 (hsizer1, 1, wx.EXPAND | wx.ALL, b),
673 ])
674
675 #really tricky ;-)
676 fgs.AddGrowableRow(0)
677 fgs.AddGrowableCol(1)
678
679 self.SetSizer(fgs)
680
681
682 class MyPanel_22(wx.Panel):
683 """- A FlexGridSizer of 9 cells (3x3) with 5 ColWins in cells
684 and 4 empty cells"""
685
686 def __init__(self, parent):
687 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
688
689 wwhite = ColWin(self, -1, wx.WHITE)
690 wblue = ColWin(self, -1, wx.BLUE)
691 wgreen = ColWin(self, -1, wx.GREEN)
692 wcyan = ColWin(self, -1, wx.CYAN)
693 wred = ColWin(self, -1, wx.RED)
694
695 hgap, vgap = 0, 0
696 nrows, ncols = 3, 3
697 fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)
698
699 b = 5
700 fgs.AddMany([(wwhite, 1, wx.EXPAND | wx.ALL, b),
701 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
702 (wblue, 1, wx.EXPAND | wx.ALL, b),
703
704 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
705 (wgreen, 1, wx.EXPAND | wx.ALL, b),
706 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
707
708 (wred, 1, wx.EXPAND | wx.ALL, b),
709 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
710 (wcyan, 1, wx.EXPAND | wx.ALL, b),
711 ])
712
713 fgs.AddGrowableRow(0)
714 fgs.AddGrowableRow(1)
715 fgs.AddGrowableRow(2)
716
717 fgs.AddGrowableCol(0)
718 fgs.AddGrowableCol(1)
719 fgs.AddGrowableCol(2)
720
721 self.SetSizer(fgs)
722
723
724 class MyPanel_23(wx.Panel):
725 """- A FlexGridSizer of 9 cells (3x3) with 4 ColWins in cells and 5 empty cells
726 - rows 1 and 3 have a fixed size"""
727
728 def __init__(self, parent):
729 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
730
731 h = 40
732 wwhite = wx.Window(self, -1, (-1, -1), (-1, h), wx.SIMPLE_BORDER)
733 wwhite.SetBackgroundColour(wx.WHITE)
734 wblue = ColWin(self, -1, wx.BLUE)
735 wgreen = ColWin(self, -1, wx.GREEN)
736 wred = wx.Window(self, -1, (-1, -1), (-1, h), wx.SIMPLE_BORDER)
737 wred.SetBackgroundColour(wx.RED)
738
739 hgap, vgap = 0, 0
740 nrows, ncols = 3, 3
741 fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)
742
743 b = 5
744 fgs.AddMany([((-1, -1), 1, wx.EXPAND | wx.ALL, b),
745 (wwhite, 1, wx.EXPAND | wx.ALL, b),
746 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
747
748 (wblue, 1, wx.EXPAND | wx.ALL, b),
749 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
750 (wgreen, 1, wx.EXPAND | wx.ALL, b),
751
752 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
753 (wred, 1, wx.EXPAND | wx.ALL, b),
754 ((-1, -1), 1, wx.EXPAND | wx.ALL, b),
755 ])
756
757 fgs.AddGrowableRow(1)
758
759 fgs.AddGrowableCol(0)
760 fgs.AddGrowableCol(1)
761 fgs.AddGrowableCol(2)
762
763 self.SetSizer(fgs)
764
765
766 class MyPanel_24(wx.Panel):
767 """- This example is coming from one of my applications
768 - in the real app, the white window is a drawing area and
769 the five lower windows are StaticTexts with a defined
770 font size (this is why I force a fixed height)"""
771
772 def __init__(self, parent):
773 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
774 self.parent = parent
775
776 #a menu for the beauty of the demo
777 menu1 = wx.Menu()
778 menu1.Append(101, '&aaa')
779 menuBar = wx.MenuBar()
780 menuBar.Append(menu1, '&File')
781 self.parent.SetMenuBar(menuBar)
782
783 wwhite = ColWin(self, -1, wx.WHITE)
784 wblue = ColWin(self, -1, wx.BLUE)
785 wgreen = ColWin(self, -1, wx.GREEN)
786 wcyan = ColWin(self, -1, wx.CYAN)
787 wred = ColWin(self, -1, wx.RED)
788 wcoral = ColWin(self, -1, wx.Colour('coral'))
789 staline = wx.StaticLine(self, -1, (-1, -1), (-1, 2), wx.LI_HORIZONTAL)
790
791 vsizer1 = wx.BoxSizer(wx.VERTICAL)
792 b = 5
793 vsizer1.Add(wblue, 1, wx.EXPAND | wx.BOTTOM, b)
794 vsizer1.Add(wgreen, 1, wx.EXPAND)
795
796 hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
797 b = 5
798 minhe = 100
799 hsizer2.Add(wcoral, 2, wx.EXPAND | wx.RIGHT, b)
800 hsizer2.Add(wcyan, 3, wx.EXPAND | wx.RIGHT, b)
801 hsizer2.Add(wred, 3, wx.EXPAND | wx.RIGHT, b)
802 hsizer2.Add(vsizer1, 2, wx.EXPAND, border=b)
803 hsizer2.SetItemMinSize(wcoral, (-1, minhe))
804 hsizer2.SetItemMinSize(wcyan, (-1, minhe))
805 hsizer2.SetItemMinSize(wred, (-1, minhe))
806 hsizer2.SetItemMinSize(vsizer1, (-1, minhe))
807
808 vsizer3 = wx.BoxSizer(wx.VERTICAL)
809 b = 5
810 vsizer3.Add(staline, 0, wx.GROW | wx.ALL, 0)
811 vsizer3.Add(wwhite, 1, wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, b)
812 vsizer3.Add(hsizer2, 0, wx.EXPAND | wx.ALL, b)
813
814 self.SetSizerAndFit(vsizer3)
815
816
817 class MyPanel_25(wx.Panel):
818 """- This shows the relaton between the font size and the size of the
819 widgets when sizers are used"""
820
821 def __init__(self, parent):
822 wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)
823 self.parent = parent
824
825 fs = 20
826 self.SetFont(wx.Font(fs, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False))
827
828
829 statxt1 = wx.StaticText(self, -1, 'one, two, three', (-1, -1), (-1, -1))
830 statxt1.SetBackgroundColour(wx.WHITE)
831 statxt2 = wx.StaticText(self, -1, 'eins, zwei, drei', (-1, -1), (-1, -1))
832 statxt3 = wx.StaticText(self, -1, 'un, deux, trois', (-1, -1), (-1, -1))
833 b1 = wx.Button(self, wx.NewIdRef(), 'button1', (-1, -1), (-1, -1))
834 b2 = wx.Button(self, wx.NewIdRef(), 'button2', (-1, -1), (-1, -1))
835
836 b = 5
837 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
838 hsizer1.Add(b1, 1, wx.EXPAND | wx.ALL, b)
839 hsizer1.Add(b2, 1, wx.EXPAND | wx.ALL, b)
840
841 vsizer1 = wx.BoxSizer(wx.VERTICAL)
842 vsizer1.Add(statxt1, 0, wx.ALL, b)
843 vsizer1.Add(statxt2, 0, wx.ALL, b)
844 vsizer1.Add(statxt3, 0, wx.ALL, b)
845 vsizer1.Add(hsizer1, 0, wx.ALL, b)
846
847 self.SetSizerAndFit(vsizer1)
848 self.parent.SetClientSize(vsizer1.GetSize())
849
850
851 class TestComboBox(wx.Panel):
852 def __init__(self, parent, id):
853 wx.Panel.__init__(self, parent, id)
854
855 sampleList = []
856 for i in range(0, 26):
857 sampleList.append('MyPanel_' + str(i))
858
859 st = wx.StaticText(self, -1, "Select the example you want to see."
860 "Then click the View button.", (-1, -1))
861
862 doctxt = wx.StaticText(self, -1, "")
863 doctxt.SetLabel(MyPanel_0.__doc__)
864 self.doctxt = doctxt
865
866 # This combobox is created with a preset list of values.
867 cb = wx.ComboBox(
868 self, -1, "MyPanel_0", (-1, -1),
869 (-1, -1), sampleList, wx.CB_DROPDOWN
870 )
871 self.cb = cb
872
873 stline = wx.StaticLine(self, -1, (-1, -1), (-1, 2), wx.LI_HORIZONTAL)
874
875 btnView = wx.Button(self, -1, "&View")
876 self.Bind(wx.EVT_BUTTON, self.OnClickView, btnView)
877
878 btnExit = wx.Button(self, -1, "E&xit")
879 self.Bind(wx.EVT_BUTTON, self.OnClickExit, btnExit)
880
881 b = 5
882 vsizer1 = wx.BoxSizer(wx.VERTICAL)
883 vsizer1.Add(st, 0, wx.EXPAND | wx.ALL, b)
884 vsizer1.Add(cb, 0, wx.EXPAND | wx.ALL, 0)
885 vsizer1.Add(doctxt, 0, wx.EXPAND | wx.ALL, b)
886
887 hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
888 hsizer1.Add(btnView, 0)
889 hsizer1.Add(btnExit, 0, wx.LEFT, b)
890
891 vsizer2 = wx.BoxSizer(wx.VERTICAL)
892 vsizer2.Add(vsizer1, 5, wx.EXPAND | wx.ALL, 0)
893 vsizer2.Add(stline, 0, wx.GROW | wx.ALL, b)
894 vsizer2.Add(hsizer1, 1, wx.ALIGN_RIGHT | wx.ALL, b)
895 self.SetSizer(vsizer2)
896
897 self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, cb)
898
899 # When the user selects something, we go here.
900 def EvtComboBox(self, evt):
901 self.doctxt.SetLabel(eval('%s.__doc__' % self.cb.GetValue()))
902
903 def OnClickView(self, evt):
904 win = wx.Frame(self, -1, self.cb.GetValue(), size=(350, 200),
905 style = wx.DEFAULT_FRAME_STYLE)
906 eval('%s(win)' % self.cb.GetValue())
907
908 win.CenterOnScreen()
909 win.Show(True)
910
911 def OnClickExit(self, evt):
912 sys.exit()
913
914
915
916 class MyFrame(wx.Frame):
917
918 def __init__(self, parent, id):
919 s = __file__
920 wx.Frame.__init__(self, parent, id, s, (0, 0), (500, 400))
921
922 self.panel = eval('MyPanel_%d(self)' % panel)
923 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
924
925 def OnCloseWindow(self, event):
926 self.Destroy()
927
928
929 class MyApp(wx.App):
930
931 def OnInit(self):
932 frame = wx.Frame(None, -1, __file__, (0, 0), (400, 300))
933 frame.panel = TestComboBox(frame, -1)
934 frame.Show(True)
935 self.SetTopWindow(frame)
936 return True
937
938
939 def main():
940 app = MyApp(False)
941 app.MainLoop()
942
943
944 if __name__ == "__main__" :
945 main()
Additional Information
Link :
- - - - -
https://wiki.wxpython.org/TitleIndex
Thanks to
Jean-Michel Fauth, the wxPython community...
About this page
Date(d/m/y) Person (bot) Comments :
27/06/05 - Jean-Michel Fauth (Created page).
26/11/23 - Ecco (Updated page and code for wxPython Phoenix).
Comments
- blah, blah, blah....