How to create a customized caption box dialog (Phoenix)
Keywords : Dialog, CaptionBox, BufferedPaintDC, GCDC.
Contents
Demonstrating :
Tested py3.x, wx4.x and Win10.
Are you ready to use some samples ?
Test, modify, correct, complete, improve and share your discoveries !
First sample
ICON file : icon_wxWidgets.ico
1 # sample_one.py
2
3 import sys
4 import os
5 import wx
6
7 # class MyCaptionBox
8 # class MyDialog
9 # class MyFrame
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 class MyCaptionBox(wx.Panel):
15 """
16 ...
17 """
18 def __init__(self, parent, caption):
19 """
20 ...
21 """
22 super(MyCaptionBox, self).__init__(parent,
23 style=wx.NO_BORDER |
24 wx.TAB_TRAVERSAL)
25
26 #------------
27
28 # Attributes.
29 self._caption = caption
30 self._csizer = wx.BoxSizer(wx.VERTICAL)
31
32 #------------
33
34 # Simplified init method.
35 self.BindEvents()
36 self.DoLayout()
37
38 #-----------------------------------------------------------------------
39
40 def BindEvents(self):
41 """
42 Binds the events to specific methods.
43 """
44
45 self.Bind(wx.EVT_PAINT, self.OnPaint)
46 self.Bind(wx.EVT_SIZE, self.OnSize)
47
48
49 def DoLayout(self):
50 """
51 ...
52 """
53
54 msizer = wx.BoxSizer(wx.HORIZONTAL)
55 self._csizer.AddSpacer(25) # Extra space for caption.
56 msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 15)
57 self.SetSizer(msizer)
58
59
60 def DoGetBestSize(self):
61 """
62 ...
63 """
64
65 self.Refresh()
66
67 size = super(MyCaptionBox, self).DoGetBestSize()
68
69 # Compensate for wide caption labels.
70 tw = self.GetTextExtent(self._caption)[0]
71 size.SetWidth(max(size.width, tw)) # Box width.
72
73 return size
74
75
76 def AddItem(self, item):
77 """
78 Add a window or sizer item to the settingCaptionBox.
79 """
80
81 self._csizer.Add(item, 0, wx.ALL, 5)
82
83
84 def OnSize(self, event):
85 """
86 ...
87 """
88
89 event.Skip()
90 self.Refresh()
91
92
93 def OnPaint(self, event):
94 """
95 Draws the Caption and border around the controls.
96 """
97
98 dc = wx.BufferedPaintDC(self)
99 dc.Clear()
100 gcdc = wx.GCDC(dc)
101
102 gc = gcdc.GetGraphicsContext()
103
104 # Get the working rectangle we can draw in.
105 rect = self.GetClientRect()
106
107 # Get the sytem color to draw the caption.
108 ss = wx.SystemSettings
109 color = "#165817"
110 txtcolor = ss.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
111 gcdc.SetTextForeground(txtcolor)
112
113 # Font, size and style for the caption.
114 font = self.GetFont().GetPointSize()
115 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
116 font.SetWeight(wx.BOLD)
117
118 # Draw the border.
119 rect.Inflate(-10, -10)
120 # Stroke color and thickness.
121 gcdc.SetPen(wx.Pen(color, 2, wx.SOLID))
122 gcdc.SetBrush(wx.TRANSPARENT_BRUSH)
123 gcdc.DrawRoundedRectangle(rect, 8)
124
125 # Gradient caption (green ---> black).
126 color1 = wx.Colour(41, 240, 44)
127 color2 = wx.Colour(0, 0, 0)
128 x1, y1 = rect.x, rect.y
129 y2 = y1 + 25
130
131 gradbrush = gc.CreateLinearGradientBrush(x1, y1,
132 x1, y2,
133 color1,
134 color2)
135 gc.SetBrush(gradbrush)
136
137 # Add the caption.
138 rect = wx.Rect(rect.x, rect.y,
139 rect.width, 26) # Caption size.
140 dc.SetBrush(wx.Brush(color))
141 gcdc.DrawRoundedRectangle(rect, 8)
142 rect.Inflate(-5, 0)
143 gcdc.SetFont(font)
144 gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
145
146 #---------------------------------------------------------------------------
147
148 class MyDialog(wx.Dialog):
149 """
150 ...
151 """
152 def __init__(self, parent, title,
153 pos=wx.DefaultPosition,
154 size=wx.DefaultSize):
155 wx.Dialog.__init__(self,
156 parent,
157 -1,
158 title,
159 pos=(-1, -1),
160 size=(420, 550),
161 style=wx.DEFAULT_FRAME_STYLE)
162
163 #------------
164
165 # Attributes.
166 self.parent = parent
167
168 #------------
169
170 # Simplified init method.
171 self.SetProperties()
172 self.CreateCtrls()
173 self.DoLayout()
174
175 #------------
176
177 self.CenterOnScreen(wx.BOTH)
178 print("\nDisplay the caption box dialog")
179
180 #------------
181
182 self.ShowModal()
183 self.Destroy()
184
185 #-----------------------------------------------------------------------
186
187 def SetProperties(self):
188 """
189 ...
190 """
191
192 frameicon = wx.Icon("icon_wxWidgets.ico")
193 self.SetIcon(frameicon)
194
195 self.SetMinSize((420, 550))
196
197
198 def CreateCtrls(self):
199 """
200 ...
201 """
202
203 self.firstBox = MyCaptionBox(self, "First caption")
204
205 # Create some checkboxes in the box.
206 for x in range(4):
207 cb = wx.CheckBox(self.firstBox,
208 label="CheckBox %d" % x)
209 self.firstBox.AddItem(cb)
210
211 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
212
213 #------------
214
215 self.secondBox = MyCaptionBox(self, "Second caption")
216
217 # Create some checkboxes in the box.
218 for x in range(4):
219 cb = wx.CheckBox(self.secondBox,
220 label="CheckBox %d" % x)
221 self.secondBox.AddItem(cb)
222
223 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
224
225 #------------
226
227 self.thirdBox = MyCaptionBox(self, "Third caption")
228
229 # Create some checkboxes in the box.
230 for x in range(4):
231 cb = wx.CheckBox(self.thirdBox,
232 label="CheckBox %d" % x)
233 self.thirdBox.AddItem(cb)
234
235 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
236
237 #------------
238
239 self.btnSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
240
241
242 def DoLayout(self):
243 """
244 ...
245 """
246
247 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
248 ctrlSizer = wx.BoxSizer(wx.VERTICAL)
249
250 ctrlSizer.Add(self.firstBox, 1, wx.EXPAND)
251 ctrlSizer.Add(self.secondBox, 1, wx.EXPAND)
252 ctrlSizer.Add(self.thirdBox, 1, wx.EXPAND)
253 ctrlSizer.Add(self.btnSizer, 0, wx.EXPAND |
254 wx.RIGHT, 5)
255
256 mainSizer.Add(ctrlSizer, 1, wx.EXPAND |
257 wx.BOTTOM, 10)
258
259 self.SetSizer(mainSizer)
260 mainSizer.Layout()
261
262
263 def OnPass(self, event):
264 """
265 Just grouping the empty event handlers together.
266 """
267
268 print("Hello !")
269
270 pass
271
272 #---------------------------------------------------------------------------
273
274 class MyFrame(wx.Frame):
275 """
276 ...
277 """
278 def __init__(self):
279 super(MyFrame, self).__init__(None,
280 -1,
281 title="")
282
283 #------------
284
285 # Return application name.
286 self.app_name = wx.GetApp().GetAppName()
287
288 #------------
289
290 # Simplified init method.
291 self.SetProperties()
292 self.CreateCtrls()
293 self.BindEvents()
294 self.DoLayout()
295
296 #------------
297
298 self.CenterOnScreen()
299
300 #-----------------------------------------------------------------------
301
302 def SetProperties(self):
303 """
304 ...
305 """
306
307 self.SetTitle(self.app_name)
308
309 #------------
310
311 frameicon = wx.Icon("icon_wxWidgets.ico")
312 self.SetIcon(frameicon)
313
314
315 def CreateCtrls(self):
316 """
317 ...
318 """
319
320 # Create a panel.
321 self.panel = wx.Panel(self, -1)
322
323 #------------
324
325 # Add some buttons.
326 self.btnDlg = wx.Button(self.panel,
327 -1,
328 "&Show caption box dialog")
329
330 self.btnClose = wx.Button(self.panel,
331 -1,
332 "&Close")
333
334
335 def BindEvents(self):
336 """
337 Bind some events to an events handler.
338 """
339
340 # Bind the close event to an event handler.
341 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
342
343 # Bind the buttons event to an event handler.
344 self.Bind(wx.EVT_BUTTON, self.OnSetting, self.btnDlg)
345 self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
346
347
348 def DoLayout(self):
349 """
350 ...
351 """
352
353 # MainSizer is the top-level one that manages everything.
354 mainSizer = wx.BoxSizer(wx.VERTICAL)
355
356 # wx.BoxSizer(window, proportion, flag, border)
357 # wx.BoxSizer(sizer, proportion, flag, border)
358 mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
359 mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
360
361 # Finally, tell the panel to use the sizer for layout.
362 self.panel.SetAutoLayout(True)
363 self.panel.SetSizer(mainSizer)
364
365 mainSizer.Fit(self.panel)
366
367 #-----------------------------------------------------------------------
368
369 def OnCloseMe(self, event):
370 """
371 ...
372 """
373
374 self.Close(True)
375
376
377 def OnSetting(self, event):
378 """
379 ...
380 """
381
382 settingDlg = MyDialog(self,
383 title="Caption box dialog")
384 #pos=(-1, -1),
385 #size=(500, 300))
386
387 def OnCloseWindow(self, event):
388 """
389 ...
390 """
391
392 self.Destroy()
393
394 #---------------------------------------------------------------------------
395
396 class MyApp(wx.App):
397 """
398 ...
399 """
400 def OnInit(self):
401
402 #------------
403
404 self.SetAppName("Main frame")
405
406 #------------
407
408 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
409
410 #------------
411
412 frame = MyFrame()
413 self.SetTopWindow(frame)
414 frame.Show(True)
415
416 return True
417
418 #-----------------------------------------------------------------------
419
420 def GetInstallDir(self):
421 """
422 Returns the installation directory for my application.
423 """
424
425 return self.installDir
426
427 #---------------------------------------------------------------------------
428
429 def main():
430 app = MyApp(False)
431 app.MainLoop()
432
433 #---------------------------------------------------------------------------
434
435 if __name__ == "__main__" :
436 main()
Second sample
ICON file : icon_wxWidgets.ico
1 # sample_two.py
2
3 import sys
4 import os
5 import wx
6
7 # class MyCaptionBox
8 # class MyDialog
9 # class MyFrame
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 class MyCaptionBox(wx.Panel):
15 """
16 ...
17 """
18 def __init__(self, parent, caption):
19 """
20 ...
21 """
22 super(MyCaptionBox, self).__init__(parent,
23 style=wx.NO_BORDER |
24 wx.TAB_TRAVERSAL)
25
26 #------------
27
28 # Attributes.
29 self._caption = caption
30 self._csizer = wx.BoxSizer(wx.VERTICAL)
31
32 #------------
33
34 # Simplified init method.
35 self.BindEvents()
36 self.DoLayout()
37
38 #-----------------------------------------------------------------------
39
40 def BindEvents(self):
41 """
42 Binds the events to specific methods.
43 """
44
45 self.Bind(wx.EVT_PAINT, self.OnPaint)
46 self.Bind(wx.EVT_SIZE, self.OnSize)
47
48
49 def DoLayout(self):
50 """
51 ...
52 """
53
54 msizer = wx.BoxSizer(wx.HORIZONTAL)
55 self._csizer.AddSpacer(25) # Extra space for caption.
56 msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 15)
57 self.SetSizer(msizer)
58
59
60 def DoGetBestSize(self):
61 """
62 ...
63 """
64
65 self.Refresh()
66
67 size = super(MyCaptionBox, self).DoGetBestSize()
68
69 # Compensate for wide caption labels.
70 tw = self.GetTextExtent(self._caption)[0]
71 size.SetWidth(max(size.width, tw)) # Box width.
72
73 return size
74
75
76 def AddItem(self, item):
77 """
78 Add a window or sizer item to the settingCaptionBox.
79 """
80
81 self._csizer.Add(item, 0, wx.ALL, 5)
82
83
84 def OnSize(self, event):
85 """
86 ...
87 """
88
89 event.Skip()
90 self.Refresh()
91
92
93 def OnPaint(self, event):
94 """
95 Draws the Caption and border around the controls.
96 """
97
98 dc = wx.BufferedPaintDC(self)
99 dc.Clear()
100 gcdc = wx.GCDC(dc)
101
102 gc = gcdc.GetGraphicsContext()
103
104 # Get the working rectangle we can draw in.
105 rect = self.GetClientRect()
106
107 # Get the sytem color to draw the caption.
108 ss = wx.SystemSettings
109 color = "#760000"
110 txtcolor = ss.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
111 gcdc.SetTextForeground(txtcolor)
112
113 # Font, size and style for the caption.
114 font = self.GetFont().GetPointSize()
115 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
116 font.SetWeight(wx.BOLD)
117
118 # Draw the border.
119 rect.Inflate(-10, -10)
120 # Stroke color and thickness.
121 gcdc.SetPen(wx.Pen(color, 2, wx.SOLID))
122 gcdc.SetBrush(wx.TRANSPARENT_BRUSH)
123 gcdc.DrawRectangle(rect)
124
125 # Gradient caption (red ---> black).
126 color1 = wx.Colour(255, 0, 0)
127 color2 = wx.Colour(0, 0, 0)
128 x1, y1 = rect.x, rect.y
129 y2 = y1 + 25
130
131 gradbrush = gc.CreateLinearGradientBrush(x1, y1,
132 x1, y2,
133 color1,
134 color2)
135 gc.SetBrush(gradbrush)
136
137 # Add the caption.
138 rect = wx.Rect(rect.x, rect.y,
139 rect.width, 26) # Caption size.
140 dc.SetBrush(wx.Brush(color))
141 gcdc.DrawRectangle(rect)
142 rect.Inflate(-5, 0)
143 gcdc.SetFont(font)
144 gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
145
146 #---------------------------------------------------------------------------
147
148 class MyDialog(wx.Dialog):
149 """
150 ...
151 """
152 def __init__(self, parent, title,
153 pos=wx.DefaultPosition,
154 size=wx.DefaultSize):
155 wx.Dialog.__init__(self,
156 parent,
157 -1,
158 title,
159 pos=(-1, -1),
160 size=(420, 550),
161 style=wx.DEFAULT_FRAME_STYLE)
162
163 #------------
164
165 # Attributes.
166 self.parent = parent
167
168 #------------
169
170 # Simplified init method.
171 self.SetProperties()
172 self.CreateCtrls()
173 self.DoLayout()
174
175 #------------
176
177 self.CenterOnScreen(wx.BOTH)
178 print("\nDisplay the caption box dialog")
179
180 #------------
181
182 self.ShowModal()
183 self.Destroy()
184
185 #-----------------------------------------------------------------------
186
187 def SetProperties(self):
188 """
189 ...
190 """
191
192 frameicon = wx.Icon("icon_wxWidgets.ico")
193 self.SetIcon(frameicon)
194
195 self.SetMinSize((420, 550))
196
197
198 def CreateCtrls(self):
199 """
200 ...
201 """
202
203 self.firstBox = MyCaptionBox(self, "First caption")
204
205 # Create some static text in the box.
206 for x in range(4):
207 st = wx.StaticText(self.firstBox,
208 label="- blah, blah, blah, blah... %d" % x)
209 self.firstBox.AddItem(st)
210
211 #------------
212
213 self.secondBox = MyCaptionBox(self, "Second caption")
214
215 # Create some static text in the box.
216 for x in range(4):
217 st = wx.StaticText(self.secondBox,
218 label="- blah, blah, blah, blah... %d" % x)
219 self.secondBox.AddItem(st)
220
221 #------------
222
223 self.thirdBox = MyCaptionBox(self, "Third caption")
224
225 # Create some static text in the box.
226 for x in range(4):
227 st = wx.StaticText(self.thirdBox,
228 label="- blah, blah, blah, blah... %d" % x)
229 self.thirdBox.AddItem(st)
230
231 #------------
232
233 self.btnSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
234
235
236 def DoLayout(self):
237 """
238 ...
239 """
240
241 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
242 ctrlSizer = wx.BoxSizer(wx.VERTICAL)
243
244 ctrlSizer.Add(self.firstBox, 1, wx.EXPAND)
245 ctrlSizer.Add(self.secondBox, 1, wx.EXPAND)
246 ctrlSizer.Add(self.thirdBox, 1, wx.EXPAND)
247 ctrlSizer.Add(self.btnSizer, 0, wx.EXPAND |
248 wx.RIGHT, 5)
249
250 mainSizer.Add(ctrlSizer, 1, wx.EXPAND |
251 wx.BOTTOM, 10)
252
253 self.SetSizer(mainSizer)
254 mainSizer.Layout()
255
256 #---------------------------------------------------------------------------
257
258 class MyFrame(wx.Frame):
259 """
260 ...
261 """
262 def __init__(self):
263 super(MyFrame, self).__init__(None,
264 -1,
265 title="")
266
267 #------------
268
269 # Return application name.
270 self.app_name = wx.GetApp().GetAppName()
271
272 #------------
273
274 # Simplified init method.
275 self.SetProperties()
276 self.CreateCtrls()
277 self.BindEvents()
278 self.DoLayout()
279
280 #------------
281
282 self.CenterOnScreen()
283
284 #-----------------------------------------------------------------------
285
286 def SetProperties(self):
287 """
288 ...
289 """
290
291 self.SetTitle(self.app_name)
292
293 #------------
294
295 frameicon = wx.Icon("icon_wxWidgets.ico")
296 self.SetIcon(frameicon)
297
298
299 def CreateCtrls(self):
300 """
301 ...
302 """
303
304 # Create a panel.
305 self.panel = wx.Panel(self, -1)
306
307 #------------
308
309 # Add some buttons.
310 self.btnDlg = wx.Button(self.panel,
311 -1,
312 "&Show caption box dialog")
313
314 self.btnClose = wx.Button(self.panel,
315 -1,
316 "&Close")
317
318
319 def BindEvents(self):
320 """
321 Bind some events to an events handler.
322 """
323
324 # Bind the close event to an event handler.
325 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
326
327 # Bind the buttons event to an event handler.
328 self.Bind(wx.EVT_BUTTON, self.OnSetting, self.btnDlg)
329 self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
330
331
332 def DoLayout(self):
333 """
334 ...
335 """
336
337 # MainSizer is the top-level one that manages everything.
338 mainSizer = wx.BoxSizer(wx.VERTICAL)
339
340 # wx.BoxSizer(window, proportion, flag, border)
341 # wx.BoxSizer(sizer, proportion, flag, border)
342 mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
343 mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
344
345 # Finally, tell the panel to use the sizer for layout.
346 self.panel.SetAutoLayout(True)
347 self.panel.SetSizer(mainSizer)
348
349 mainSizer.Fit(self.panel)
350
351 #-----------------------------------------------------------------------
352
353 def OnCloseMe(self, event):
354 """
355 ...
356 """
357
358 self.Close(True)
359
360
361 def OnSetting(self, event):
362 """
363 ...
364 """
365
366 settingDlg = MyDialog(self,
367 title="Caption box dialog")
368 #pos=(-1, -1),
369 #size=(500, 300))
370
371 def OnCloseWindow(self, event):
372 """
373 ...
374 """
375
376 self.Destroy()
377
378 #---------------------------------------------------------------------------
379
380 class MyApp(wx.App):
381 """
382 ...
383 """
384 def OnInit(self):
385
386 #------------
387
388 self.SetAppName("Main frame")
389
390 #------------
391
392 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
393
394 #------------
395
396 frame = MyFrame()
397 self.SetTopWindow(frame)
398 frame.Show(True)
399
400 return True
401
402 #-----------------------------------------------------------------------
403
404 def GetInstallDir(self):
405 """
406 Returns the installation directory for my application.
407 """
408
409 return self.installDir
410
411 #---------------------------------------------------------------------------
412
413 def main():
414 app = MyApp(False)
415 app.MainLoop()
416
417 #---------------------------------------------------------------------------
418
419 if __name__ == "__main__" :
420 main()
Third sample
ICON file : icon_wxWidgets.ico
1 # sample_three.py
2
3 import sys
4 import os
5 import wx
6
7 # class MyCaptionBox
8 # class MyDialog
9 # class MyFrame
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 class MyCaptionBox(wx.Panel):
15 """
16 ...
17 """
18 def __init__(self, parent, caption):
19 """
20 ...
21 """
22 super(MyCaptionBox, self).__init__(parent,
23 style=wx.NO_BORDER |
24 wx.TAB_TRAVERSAL)
25
26 #------------
27
28 # Attributes.
29 self._caption = caption
30 self._csizer = wx.BoxSizer(wx.VERTICAL)
31
32 #------------
33
34 # Simplified init method.
35 self.BindEvents()
36 self.DoLayout()
37
38 #-----------------------------------------------------------------------
39
40 def BindEvents(self):
41 """
42 Binds the events to specific methods.
43 """
44
45 self.Bind(wx.EVT_PAINT, self.OnPaint)
46 self.Bind(wx.EVT_SIZE, self.OnSize)
47
48
49 def DoLayout(self):
50 """
51 ...
52 """
53
54 msizer = wx.BoxSizer(wx.HORIZONTAL)
55 self._csizer.AddSpacer(25) # Extra space for caption.
56 msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 15)
57 self.SetSizer(msizer)
58
59
60 def DoGetBestSize(self):
61 """
62 ...
63 """
64
65 self.Refresh()
66
67 size = super(MyCaptionBox, self).DoGetBestSize()
68
69 # Compensate for wide caption labels.
70 tw = self.GetTextExtent(self._caption)[0]
71 size.SetWidth(max(size.width, tw)) # Box width.
72
73 return size
74
75
76 def AddItem(self, item):
77 """
78 Add a window or sizer item to the settingCaptionBox.
79 """
80
81 self._csizer.Add(item, 0, wx.ALL, 5)
82
83
84 def OnSize(self, event):
85 """
86 ...
87 """
88
89 event.Skip()
90 self.Refresh()
91
92
93 def OnPaint(self, event):
94 """
95 Draws the Caption and border around the controls.
96 """
97
98 dc = wx.BufferedPaintDC(self)
99 dc.Clear()
100 gcdc = wx.GCDC(dc)
101
102 gc = gcdc.GetGraphicsContext()
103
104 # Get the working rectangle we can draw in.
105 rect = self.GetClientRect()
106
107 # Get the sytem color to draw the caption.
108 ss = wx.SystemSettings
109 color = "#1a647f"
110 txtcolor = ss.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
111 gcdc.SetTextForeground(txtcolor)
112
113 # Font, size and style for the caption.
114 font = self.GetFont().GetPointSize()
115 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
116 font.SetWeight(wx.BOLD)
117
118 # Draw the border.
119 rect.Inflate(-10, -10)
120 # Stroke color and thickness.
121 gcdc.SetPen(wx.Pen(color, 2, wx.SOLID))
122 gcdc.SetBrush(wx.TRANSPARENT_BRUSH)
123 gcdc.DrawRectangle(rect.x, rect.y+18, rect.width, rect.height-18)
124
125 # Gradient caption (blue ---> black).
126 color1 = wx.Colour(51, 200, 253)
127 color2 = wx.Colour(0, 0, 0)
128 x1, y1 = rect.x, rect.y
129 y2 = y1 + 25
130
131 gradbrush = gc.CreateLinearGradientBrush(x1, y1,
132 x1, y2,
133 color1,
134 color2)
135 gc.SetBrush(gradbrush)
136
137 # Add the caption.
138 rect = wx.Rect(rect.x, rect.y,
139 rect.width, 26) # Caption size.
140 dc.SetBrush(wx.Brush(color))
141 gcdc.DrawRoundedRectangle(rect, 8)
142 gcdc.SetPen(wx.TRANSPARENT_PEN)
143 gcdc.DrawRectangle(rect.x+1, rect.y+15, rect.width-3, rect.height-14)
144 rect.Inflate(-5, 0)
145 gcdc.SetFont(font)
146 gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
147
148 #---------------------------------------------------------------------------
149
150 class MyDialog(wx.Dialog):
151 """
152 ...
153 """
154 def __init__(self, parent, title,
155 pos=wx.DefaultPosition,
156 size=wx.DefaultSize):
157 wx.Dialog.__init__(self,
158 parent,
159 -1,
160 title,
161 pos=(-1, -1),
162 size=(420, 550),
163 style=wx.DEFAULT_FRAME_STYLE)
164
165 #------------
166
167 # Attributes.
168 self.parent = parent
169
170 #------------
171
172 # Simplified init method.
173 self.SetProperties()
174 self.CreateCtrls()
175 self.DoLayout()
176
177 #------------
178
179 self.CenterOnScreen(wx.BOTH)
180 print("\nDisplay the caption box dialog")
181
182 #------------
183
184 self.ShowModal()
185 self.Destroy()
186
187 #-----------------------------------------------------------------------
188
189 def SetProperties(self):
190 """
191 ...
192 """
193
194 frameicon = wx.Icon("icon_wxWidgets.ico")
195 self.SetIcon(frameicon)
196
197 self.SetMinSize((420, 550))
198
199
200 def CreateCtrls(self):
201 """
202 ...
203 """
204
205 self.firstBox = MyCaptionBox(self, "First caption")
206
207 # Create some checkboxes in the box.
208 for x in range(4):
209 cb = wx.CheckBox(self.firstBox,
210 label="CheckBox %d" % x)
211 self.firstBox.AddItem(cb)
212
213 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
214
215 #------------
216
217 self.secondBox = MyCaptionBox(self, "Second caption")
218
219 # Create some checkboxes in the box.
220 for x in range(4):
221 cb = wx.CheckBox(self.secondBox,
222 label="CheckBox %d" % x)
223 self.secondBox.AddItem(cb)
224
225 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
226
227 #------------
228
229 self.thirdBox = MyCaptionBox(self, "Third caption")
230
231 # Create some checkboxes in the box.
232 for x in range(4):
233 cb = wx.CheckBox(self.thirdBox,
234 label="CheckBox %d" % x)
235 self.thirdBox.AddItem(cb)
236
237 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
238
239 #------------
240
241 self.btnSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
242
243
244 def DoLayout(self):
245 """
246 ...
247 """
248
249 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
250 ctrlSizer = wx.BoxSizer(wx.VERTICAL)
251
252 ctrlSizer.Add(self.firstBox, 1, wx.EXPAND)
253 ctrlSizer.Add(self.secondBox, 1, wx.EXPAND)
254 ctrlSizer.Add(self.thirdBox, 1, wx.EXPAND)
255 ctrlSizer.Add(self.btnSizer, 0, wx.EXPAND |
256 wx.RIGHT, 5)
257
258 mainSizer.Add(ctrlSizer, 1, wx.EXPAND |
259 wx.BOTTOM, 10)
260
261 self.SetSizer(mainSizer)
262 mainSizer.Layout()
263
264
265 def OnPass(self, event):
266 """
267 Just grouping the empty event handlers together.
268 """
269
270 print("Hello !")
271
272 pass
273
274 #---------------------------------------------------------------------------
275
276 class MyFrame(wx.Frame):
277 """
278 ...
279 """
280 def __init__(self):
281 super(MyFrame, self).__init__(None,
282 -1,
283 title="")
284
285 #------------
286
287 # Return application name.
288 self.app_name = wx.GetApp().GetAppName()
289
290 #------------
291
292 # Simplified init method.
293 self.SetProperties()
294 self.CreateCtrls()
295 self.BindEvents()
296 self.DoLayout()
297
298 #------------
299
300 self.CenterOnScreen()
301
302 #-----------------------------------------------------------------------
303
304 def SetProperties(self):
305 """
306 ...
307 """
308
309 self.SetTitle(self.app_name)
310
311 #------------
312
313 frameicon = wx.Icon("icon_wxWidgets.ico")
314 self.SetIcon(frameicon)
315
316
317 def CreateCtrls(self):
318 """
319 ...
320 """
321
322 # Create a panel.
323 self.panel = wx.Panel(self, -1)
324
325 #------------
326
327 # Add some buttons.
328 self.btnDlg = wx.Button(self.panel,
329 -1,
330 "&Show caption box dialog")
331
332 self.btnClose = wx.Button(self.panel,
333 -1,
334 "&Close")
335
336
337 def BindEvents(self):
338 """
339 Bind some events to an events handler.
340 """
341
342 # Bind the close event to an event handler.
343 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
344
345 # Bind the buttons event to an event handler.
346 self.Bind(wx.EVT_BUTTON, self.OnSetting, self.btnDlg)
347 self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
348
349
350 def DoLayout(self):
351 """
352 ...
353 """
354
355 # MainSizer is the top-level one that manages everything.
356 mainSizer = wx.BoxSizer(wx.VERTICAL)
357
358 # wx.BoxSizer(window, proportion, flag, border)
359 # wx.BoxSizer(sizer, proportion, flag, border)
360 mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
361 mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
362
363 # Finally, tell the panel to use the sizer for layout.
364 self.panel.SetAutoLayout(True)
365 self.panel.SetSizer(mainSizer)
366
367 mainSizer.Fit(self.panel)
368
369 #-----------------------------------------------------------------------
370
371 def OnCloseMe(self, event):
372 """
373 ...
374 """
375
376 self.Close(True)
377
378
379 def OnSetting(self, event):
380 """
381 ...
382 """
383
384 settingDlg = MyDialog(self,
385 title="Caption box dialog")
386 #pos=(-1, -1),
387 #size=(500, 300))
388
389 def OnCloseWindow(self, event):
390 """
391 ...
392 """
393
394 self.Destroy()
395
396 #---------------------------------------------------------------------------
397
398 class MyApp(wx.App):
399 """
400 ...
401 """
402 def OnInit(self):
403
404 #------------
405
406 self.SetAppName("Main frame")
407
408 #------------
409
410 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
411
412 #------------
413
414 frame = MyFrame()
415 self.SetTopWindow(frame)
416 frame.Show(True)
417
418 return True
419
420 #-----------------------------------------------------------------------
421
422 def GetInstallDir(self):
423 """
424 Returns the installation directory for my application.
425 """
426
427 return self.installDir
428
429 #---------------------------------------------------------------------------
430
431 def main():
432 app = MyApp(False)
433 app.MainLoop()
434
435 #---------------------------------------------------------------------------
436
437 if __name__ == "__main__" :
438 main()
Fourth sample
ICON file : icon_wxWidgets.ico
1 # sample_four.py
2
3 import sys
4 import os
5 import wx
6
7 # class MyCaptionBox
8 # class MyDialog
9 # class MyFrame
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 class MyCaptionBox(wx.Panel):
15 """
16 ...
17 """
18 def __init__(self, parent, caption):
19 """
20 ...
21 """
22 super(MyCaptionBox, self).__init__(parent,
23 style=wx.NO_BORDER |
24 wx.TAB_TRAVERSAL)
25
26 #------------
27
28 # Attributes.
29 self._caption = caption
30 self._csizer = wx.BoxSizer(wx.VERTICAL)
31
32 #------------
33
34 # Simplified init method.
35 self.BindEvents()
36 self.DoLayout()
37
38 #-----------------------------------------------------------------------
39
40 def BindEvents(self):
41 """
42 Binds the events to specific methods.
43 """
44
45 self.Bind(wx.EVT_PAINT, self.OnPaint)
46 self.Bind(wx.EVT_SIZE, self.OnSize)
47
48
49 def DoLayout(self):
50 """
51 ...
52 """
53
54 msizer = wx.BoxSizer(wx.HORIZONTAL)
55 self._csizer.AddSpacer(25) # Extra space for caption.
56 msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 15)
57 self.SetSizer(msizer)
58
59
60 def DoGetBestSize(self):
61 """
62 ...
63 """
64
65 self.Refresh()
66
67 size = super(MyCaptionBox, self).DoGetBestSize()
68
69 # Compensate for wide caption labels.
70 tw = self.GetTextExtent(self._caption)[0]
71 size.SetWidth(max(size.width, tw)) # Box width.
72
73 return size
74
75
76 def AddItem(self, item):
77 """
78 Add a window or sizer item to the settingCaptionBox.
79 """
80
81 self._csizer.Add(item, 0, wx.ALL, 5)
82
83
84 def OnSize(self, event):
85 """
86 ...
87 """
88
89 event.Skip()
90 self.Refresh()
91
92
93 def OnPaint(self, event):
94 """
95 Draws the Caption and border around the controls.
96 """
97
98 dc = wx.BufferedPaintDC(self)
99 dc.Clear()
100 gcdc = wx.GCDC(dc)
101
102 gc = gcdc.GetGraphicsContext()
103
104 # Get the working rectangle we can draw in.
105 rect = self.GetClientRect()
106
107 # Get the sytem color to draw the caption.
108 ss = wx.SystemSettings
109 color = "#b16211"
110 txtcolor = ss.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
111 gcdc.SetTextForeground(txtcolor)
112
113 # Font, size and style for the caption.
114 font = self.GetFont().GetPointSize()
115 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
116 font.SetWeight(wx.BOLD)
117
118 # Draw the border.
119 rect.Inflate(-10, -10)
120 # Stroke color and thickness.
121 gcdc.SetPen(wx.Pen(color, 2, wx.SOLID))
122 gcdc.SetBrush(wx.TRANSPARENT_BRUSH)
123 gcdc.DrawRoundedRectangle(rect, 8)
124
125 # Gradient caption (orange ---> black).
126 color1 = wx.Colour(255, 100, 20)
127 color2 = wx.Colour(0, 0, 0)
128 x1, y1 = rect.x, rect.y
129 y2 = y1 + 25
130
131 gradbrush = gc.CreateLinearGradientBrush(x1, y1,
132 x1, y2,
133 color1,
134 color2)
135 gc.SetBrush(gradbrush)
136
137 # Add the caption.
138 rect = wx.Rect(rect.x, rect.y,
139 rect.width, 26) # Caption size.
140 dc.SetBrush(wx.Brush(color))
141 gcdc.DrawRoundedRectangle(rect, 8)
142 gcdc.SetPen(wx.TRANSPARENT_PEN)
143 gcdc.DrawRectangle(rect.x+1, rect.y+15, rect.width-3, rect.height-14)
144 rect.Inflate(-5, 0)
145 gcdc.SetFont(font)
146 gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
147
148 #---------------------------------------------------------------------------
149
150 class MyDialog(wx.Dialog):
151 """
152 ...
153 """
154 def __init__(self, parent, title,
155 pos=wx.DefaultPosition,
156 size=wx.DefaultSize):
157 wx.Dialog.__init__(self,
158 parent,
159 -1,
160 title,
161 pos=(-1, -1),
162 size=(420, 550),
163 style=wx.DEFAULT_FRAME_STYLE)
164
165 #------------
166
167 # Attributes.
168 self.parent = parent
169
170 #------------
171
172 # Simplified init method.
173 self.SetProperties()
174 self.CreateCtrls()
175 self.DoLayout()
176
177 #------------
178
179 self.CenterOnScreen(wx.BOTH)
180 print("\nDisplay the caption box dialog")
181
182 #------------
183
184 self.ShowModal()
185 self.Destroy()
186
187 #-----------------------------------------------------------------------
188
189 def SetProperties(self):
190 """
191 ...
192 """
193
194 frameicon = wx.Icon("icon_wxWidgets.ico")
195 self.SetIcon(frameicon)
196
197 self.SetMinSize((420, 550))
198
199
200 def CreateCtrls(self):
201 """
202 ...
203 """
204
205 self.firstBox = MyCaptionBox(self, "First caption")
206
207 # Create some static text in the box.
208 for x in range(4):
209 st = wx.StaticText(self.firstBox,
210 label="- blah, blah, blah, blah... %d" % x)
211 self.firstBox.AddItem(st)
212
213 #------------
214
215 self.secondBox = MyCaptionBox(self, "Second caption")
216
217 # Create some static text in the box.
218 for x in range(4):
219 st = wx.StaticText(self.secondBox,
220 label="- blah, blah, blah, blah... %d" % x)
221 self.secondBox.AddItem(st)
222
223 #------------
224
225 self.thirdBox = MyCaptionBox(self, "Third caption")
226
227 # Create some static text in the box.
228 for x in range(4):
229 st = wx.StaticText(self.thirdBox,
230 label="- blah, blah, blah, blah... %d" % x)
231 self.thirdBox.AddItem(st)
232
233 #------------
234
235 self.btnSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
236
237
238 def DoLayout(self):
239 """
240 ...
241 """
242
243 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
244 ctrlSizer = wx.BoxSizer(wx.VERTICAL)
245
246 ctrlSizer.Add(self.firstBox, 1, wx.EXPAND)
247 ctrlSizer.Add(self.secondBox, 1, wx.EXPAND)
248 ctrlSizer.Add(self.thirdBox, 1, wx.EXPAND)
249 ctrlSizer.Add(self.btnSizer, 0, wx.EXPAND |
250 wx.RIGHT, 5)
251
252 mainSizer.Add(ctrlSizer, 1, wx.EXPAND |
253 wx.BOTTOM, 10)
254
255 self.SetSizer(mainSizer)
256 mainSizer.Layout()
257
258 #---------------------------------------------------------------------------
259
260 class MyFrame(wx.Frame):
261 """
262 ...
263 """
264 def __init__(self):
265 super(MyFrame, self).__init__(None,
266 -1,
267 title="")
268
269 #------------
270
271 # Return application name.
272 self.app_name = wx.GetApp().GetAppName()
273
274 #------------
275
276 # Simplified init method.
277 self.SetProperties()
278 self.CreateCtrls()
279 self.BindEvents()
280 self.DoLayout()
281
282 #------------
283
284 self.CenterOnScreen()
285
286 #-----------------------------------------------------------------------
287
288 def SetProperties(self):
289 """
290 ...
291 """
292
293 self.SetTitle(self.app_name)
294
295 #------------
296
297 frameicon = wx.Icon("icon_wxWidgets.ico")
298 self.SetIcon(frameicon)
299
300
301 def CreateCtrls(self):
302 """
303 ...
304 """
305
306 # Create a panel.
307 self.panel = wx.Panel(self, -1)
308
309 #------------
310
311 # Add some buttons.
312 self.btnDlg = wx.Button(self.panel,
313 -1,
314 "&Show caption box dialog")
315
316 self.btnClose = wx.Button(self.panel,
317 -1,
318 "&Close")
319
320
321 def BindEvents(self):
322 """
323 Bind some events to an events handler.
324 """
325
326 # Bind the close event to an event handler.
327 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
328
329 # Bind the buttons event to an event handler.
330 self.Bind(wx.EVT_BUTTON, self.OnSetting, self.btnDlg)
331 self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
332
333
334 def DoLayout(self):
335 """
336 ...
337 """
338
339 # MainSizer is the top-level one that manages everything.
340 mainSizer = wx.BoxSizer(wx.VERTICAL)
341
342 # wx.BoxSizer(window, proportion, flag, border)
343 # wx.BoxSizer(sizer, proportion, flag, border)
344 mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
345 mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
346
347 # Finally, tell the panel to use the sizer for layout.
348 self.panel.SetAutoLayout(True)
349 self.panel.SetSizer(mainSizer)
350
351 mainSizer.Fit(self.panel)
352
353 #-----------------------------------------------------------------------
354
355 def OnCloseMe(self, event):
356 """
357 ...
358 """
359
360 self.Close(True)
361
362
363 def OnSetting(self, event):
364 """
365 ...
366 """
367
368 settingDlg = MyDialog(self,
369 title="Caption box dialog")
370 #pos=(-1, -1),
371 #size=(500, 300))
372
373 def OnCloseWindow(self, event):
374 """
375 ...
376 """
377
378 self.Destroy()
379
380 #---------------------------------------------------------------------------
381
382 class MyApp(wx.App):
383 """
384 ...
385 """
386 def OnInit(self):
387
388 #------------
389
390 self.SetAppName("Main frame")
391
392 #------------
393
394 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
395
396 #------------
397
398 frame = MyFrame()
399 self.SetTopWindow(frame)
400 frame.Show(True)
401
402 return True
403
404 #-----------------------------------------------------------------------
405
406 def GetInstallDir(self):
407 """
408 Returns the installation directory for my application.
409 """
410
411 return self.installDir
412
413 #---------------------------------------------------------------------------
414
415 def main():
416 app = MyApp(False)
417 app.MainLoop()
418
419 #---------------------------------------------------------------------------
420
421 if __name__ == "__main__" :
422 main()
Fifth sample
ICON file : icon_wxWidgets.ico
1 # sample_five.py
2
3 import sys
4 import os
5 import wx
6
7 # class MyCaptionBox
8 # class MyDialog
9 # class MyFrame
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 class MyCaptionBox(wx.Panel):
15 """
16 ...
17 """
18 def __init__(self, parent, caption):
19 """
20 ...
21 """
22 super(MyCaptionBox, self).__init__(parent,
23 style=wx.NO_BORDER |
24 wx.TAB_TRAVERSAL)
25
26 #------------
27
28 # Attributes.
29 self._caption = caption
30 self._csizer = wx.BoxSizer(wx.VERTICAL)
31
32 #------------
33
34 # Simplified init method.
35 self.BindEvents()
36 self.DoLayout()
37
38 #-----------------------------------------------------------------------
39
40 def BindEvents(self):
41 """
42 Binds the events to specific methods.
43 """
44
45 self.Bind(wx.EVT_PAINT, self.OnPaint)
46 self.Bind(wx.EVT_SIZE, self.OnSize)
47
48
49 def DoLayout(self):
50 """
51 ...
52 """
53
54 msizer = wx.BoxSizer(wx.HORIZONTAL)
55 self._csizer.AddSpacer(25) # Extra space for caption.
56 msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 15)
57 self.SetSizer(msizer)
58
59
60 def DoGetBestSize(self):
61 """
62 ...
63 """
64
65 self.Refresh()
66
67 size = super(MyCaptionBox, self).DoGetBestSize()
68
69 # Compensate for wide caption labels.
70 tw = self.GetTextExtent(self._caption)[0]
71 size.SetWidth(max(size.width, tw)) # Box width.
72
73 return size
74
75
76 def AddItem(self, item):
77 """
78 Add a window or sizer item to the settingCaptionBox.
79 """
80
81 self._csizer.Add(item, 0, wx.ALL, 5)
82
83
84 def OnSize(self, event):
85 """
86 ...
87 """
88
89 event.Skip()
90 self.Refresh()
91
92
93 def OnPaint(self, event):
94 """
95 Draws the Caption and border around the controls.
96 """
97
98 dc = wx.BufferedPaintDC(self)
99 dc.Clear()
100 gcdc = wx.GCDC(dc)
101
102 gc = gcdc.GetGraphicsContext()
103
104 # Get the working rectangle we can draw in.
105 rect = self.GetClientRect()
106
107 # Get the sytem color to draw the caption.
108 ss = wx.SystemSettings
109 color = "#703ebb"
110 txtcolor = ss.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
111 gcdc.SetTextForeground(txtcolor)
112
113 # Font, size and style for the caption.
114 font = self.GetFont().GetPointSize()
115 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
116 font.SetWeight(wx.BOLD)
117
118 # Draw the border.
119 rect.Inflate(-10, -10)
120 # Stroke color and thickness.
121 gcdc.SetPen(wx.Pen(color, 2, wx.SOLID))
122 gcdc.SetBrush(wx.TRANSPARENT_BRUSH)
123 gcdc.DrawRectangle(rect.x, rect.y+25, rect.width, rect.height-25)
124
125 gcdc.SetPen(wx.Pen(color, 2))
126 gcdc.DrawLine(rect.x, rect.y+18, rect.x, rect.y+25)
127 gcdc.DrawLine(rect.x+202, rect.y+18, rect.x+202, rect.y+25)
128
129 # Gradient caption (purple ---> black).
130 color1 = wx.Colour(196, 115, 199)
131 color2 = wx.Colour(0, 0, 0)
132 x1, y1 = rect.x, rect.y
133 y2 = y1 + 25
134
135 gradbrush = gc.CreateLinearGradientBrush(x1, y1,
136 x1, y2,
137 color1,
138 color2)
139 gc.SetBrush(gradbrush)
140
141 # Add the caption.
142 rect = wx.Rect(rect.x, rect.y,
143 rect.width, 26) # Caption size.
144 dc.SetBrush(wx.Brush(color))
145 gcdc.DrawRoundedRectangle(rect.x, rect.y, 200+3, rect.height, 8)
146 gcdc.SetPen(wx.TRANSPARENT_PEN)
147 gcdc.DrawRectangle(rect.x+1, rect.y+12, 200, rect.height-12)
148 rect.Inflate(-5, 0)
149 gcdc.SetFont(font)
150 gcdc.DrawText(self._caption, rect.x+3, rect.y+3)
151
152 #---------------------------------------------------------------------------
153
154 class MyDialog(wx.Dialog):
155 """
156 ...
157 """
158 def __init__(self, parent, title,
159 pos=wx.DefaultPosition,
160 size=wx.DefaultSize):
161 wx.Dialog.__init__(self,
162 parent,
163 -1,
164 title,
165 pos=(-1, -1),
166 size=(420, 550),
167 style=wx.DEFAULT_FRAME_STYLE)
168
169 #------------
170
171 # Attributes.
172 self.parent = parent
173
174 #------------
175
176 # Simplified init method.
177 self.SetProperties()
178 self.CreateCtrls()
179 self.DoLayout()
180
181 #------------
182
183 self.CenterOnScreen(wx.BOTH)
184 print("\nDisplay the caption box dialog")
185
186 #------------
187
188 self.ShowModal()
189 self.Destroy()
190
191 #-----------------------------------------------------------------------
192
193 def SetProperties(self):
194 """
195 ...
196 """
197
198 frameicon = wx.Icon("icon_wxWidgets.ico")
199 self.SetIcon(frameicon)
200
201 self.SetMinSize((420, 550))
202
203
204 def CreateCtrls(self):
205 """
206 ...
207 """
208
209 self.firstBox = MyCaptionBox(self, "First caption")
210
211 # Create some checkboxes in the box.
212 for x in range(4):
213 cb = wx.CheckBox(self.firstBox,
214 label="CheckBox %d" % x)
215 self.firstBox.AddItem(cb)
216
217 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
218
219 #------------
220
221 self.secondBox = MyCaptionBox(self, "Second caption")
222
223 # Create some checkboxes in the box.
224 for x in range(4):
225 cb = wx.CheckBox(self.secondBox,
226 label="CheckBox %d" % x)
227 self.secondBox.AddItem(cb)
228
229 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
230
231 #------------
232
233 self.thirdBox = MyCaptionBox(self, "Third caption")
234
235 # Create some checkboxes in the box.
236 for x in range(4):
237 cb = wx.CheckBox(self.thirdBox,
238 label="CheckBox %d" % x)
239 self.thirdBox.AddItem(cb)
240
241 self.Bind(wx.EVT_CHECKBOX, self.OnPass, cb)
242
243 #------------
244
245 self.btnSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
246
247
248 def DoLayout(self):
249 """
250 ...
251 """
252
253 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
254 ctrlSizer = wx.BoxSizer(wx.VERTICAL)
255
256 ctrlSizer.Add(self.firstBox, 1, wx.EXPAND)
257 ctrlSizer.Add(self.secondBox, 1, wx.EXPAND)
258 ctrlSizer.Add(self.thirdBox, 1, wx.EXPAND)
259 ctrlSizer.Add(self.btnSizer, 0, wx.EXPAND |
260 wx.RIGHT, 5)
261
262 mainSizer.Add(ctrlSizer, 1, wx.EXPAND |
263 wx.BOTTOM, 10)
264
265 self.SetSizer(mainSizer)
266 mainSizer.Layout()
267
268
269 def OnPass(self, event):
270 """
271 Just grouping the empty event handlers together.
272 """
273
274 print("Hello !")
275
276 pass
277
278 #---------------------------------------------------------------------------
279
280 class MyFrame(wx.Frame):
281 """
282 ...
283 """
284 def __init__(self):
285 super(MyFrame, self).__init__(None,
286 -1,
287 title="")
288
289 #------------
290
291 # Return application name.
292 self.app_name = wx.GetApp().GetAppName()
293
294 #------------
295
296 # Simplified init method.
297 self.SetProperties()
298 self.CreateCtrls()
299 self.BindEvents()
300 self.DoLayout()
301
302 #------------
303
304 self.CenterOnScreen()
305
306 #-----------------------------------------------------------------------
307
308 def SetProperties(self):
309 """
310 ...
311 """
312
313 self.SetTitle(self.app_name)
314
315 #------------
316
317 frameicon = wx.Icon("icon_wxWidgets.ico")
318 self.SetIcon(frameicon)
319
320
321 def CreateCtrls(self):
322 """
323 ...
324 """
325
326 # Create a panel.
327 self.panel = wx.Panel(self, -1)
328
329 #------------
330
331 # Add some buttons.
332 self.btnDlg = wx.Button(self.panel,
333 -1,
334 "&Show caption box dialog")
335
336 self.btnClose = wx.Button(self.panel,
337 -1,
338 "&Close")
339
340
341 def BindEvents(self):
342 """
343 Bind some events to an events handler.
344 """
345
346 # Bind the close event to an event handler.
347 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
348
349 # Bind the buttons event to an event handler.
350 self.Bind(wx.EVT_BUTTON, self.OnSetting, self.btnDlg)
351 self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
352
353
354 def DoLayout(self):
355 """
356 ...
357 """
358
359 # MainSizer is the top-level one that manages everything.
360 mainSizer = wx.BoxSizer(wx.VERTICAL)
361
362 # wx.BoxSizer(window, proportion, flag, border)
363 # wx.BoxSizer(sizer, proportion, flag, border)
364 mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
365 mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
366
367 # Finally, tell the panel to use the sizer for layout.
368 self.panel.SetAutoLayout(True)
369 self.panel.SetSizer(mainSizer)
370
371 mainSizer.Fit(self.panel)
372
373 #-----------------------------------------------------------------------
374
375 def OnCloseMe(self, event):
376 """
377 ...
378 """
379
380 self.Close(True)
381
382
383 def OnSetting(self, event):
384 """
385 ...
386 """
387
388 settingDlg = MyDialog(self,
389 title="Caption box dialog")
390 #pos=(-1, -1),
391 #size=(500, 300))
392
393 def OnCloseWindow(self, event):
394 """
395 ...
396 """
397
398 self.Destroy()
399
400 #---------------------------------------------------------------------------
401
402 class MyApp(wx.App):
403 """
404 ...
405 """
406 def OnInit(self):
407
408 #------------
409
410 self.SetAppName("Main frame")
411
412 #------------
413
414 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
415
416 #------------
417
418 frame = MyFrame()
419 self.SetTopWindow(frame)
420 frame.Show(True)
421
422 return True
423
424 #-----------------------------------------------------------------------
425
426 def GetInstallDir(self):
427 """
428 Returns the installation directory for my application.
429 """
430
431 return self.installDir
432
433 #---------------------------------------------------------------------------
434
435 def main():
436 app = MyApp(False)
437 app.MainLoop()
438
439 #---------------------------------------------------------------------------
440
441 if __name__ == "__main__" :
442 main()
Sixth sample
ICON file : icon_wxWidgets.ico
1 # sample_six.py
2
3 import sys
4 import os
5 import wx
6
7 # class MyCaptionBox
8 # class MyDialog
9 # class MyFrame
10 # class MyApp
11
12 #---------------------------------------------------------------------------
13
14 class MyCaptionBox(wx.Panel):
15 """
16 ...
17 """
18 def __init__(self, parent, caption):
19 """
20 ...
21 """
22 super(MyCaptionBox, self).__init__(parent,
23 style=wx.NO_BORDER |
24 wx.TAB_TRAVERSAL)
25
26 #------------
27
28 # Attributes.
29 self._caption = caption
30 self._csizer = wx.BoxSizer(wx.VERTICAL)
31
32 #------------
33
34 # Simplified init method.
35 self.BindEvents()
36 self.DoLayout()
37
38 #-----------------------------------------------------------------------
39
40 def BindEvents(self):
41 """
42 Binds the events to specific methods.
43 """
44
45 self.Bind(wx.EVT_PAINT, self.OnPaint)
46 self.Bind(wx.EVT_SIZE, self.OnSize)
47
48
49 def DoLayout(self):
50 """
51 ...
52 """
53
54 msizer = wx.BoxSizer(wx.HORIZONTAL)
55 self._csizer.AddSpacer(25) # Extra space for caption.
56 msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 15)
57 self.SetSizer(msizer)
58
59
60 def DoGetBestSize(self):
61 """
62 ...
63 """
64
65 self.Refresh()
66
67 size = super(MyCaptionBox, self).DoGetBestSize()
68
69 # Compensate for wide caption labels.
70 tw = self.GetTextExtent(self._caption)[0]
71 size.SetWidth(max(size.width, tw)) # Box width.
72
73 return size
74
75
76 def AddItem(self, item):
77 """
78 Add a window or sizer item to the settingCaptionBox.
79 """
80
81 self._csizer.Add(item, 0, wx.ALL, 5)
82
83
84 def OnSize(self, event):
85 """
86 ...
87 """
88
89 event.Skip()
90 self.Refresh()
91
92
93 def OnPaint(self, event):
94 """
95 Draws the Caption and border around the controls.
96 """
97
98 dc = wx.BufferedPaintDC(self)
99 dc.Clear()
100 gcdc = wx.GCDC(dc)
101
102 gc = gcdc.GetGraphicsContext()
103
104 # Get the working rectangle we can draw in.
105 rect = self.GetClientRect()
106
107 # Get the sytem color to draw the caption.
108 ss = wx.SystemSettings
109 color = "#646464"
110 txtcolor = ss.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
111 gcdc.SetTextForeground(txtcolor)
112
113 # Font, size and style for the caption.
114 font = self.GetFont().GetPointSize()
115 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
116 font.SetWeight(wx.BOLD)
117
118 # Draw the border.
119 rect.Inflate(-10, -10)
120 # Stroke color and thickness.
121 gcdc.SetPen(wx.Pen(color, 2, wx.SOLID))
122 gcdc.SetBrush(wx.TRANSPARENT_BRUSH)
123 gcdc.DrawRectangle(rect.x, rect.y+25, rect.width, rect.height-25)
124
125 # Gradient caption (gray ---> black).
126 color1 = wx.Colour(180, 180, 180)
127 color2 = wx.Colour(0, 0, 0)
128 x1, y1 = rect.x, rect.y
129 y2 = y1 + 25
130
131 gradbrush = gc.CreateLinearGradientBrush(x1, y1,
132 x1, y2,
133 color1,
134 color2)
135 gc.SetBrush(gradbrush)
136
137 # Add the caption.
138 rect = wx.Rect(rect.x, rect.y,
139 rect.width, 26) # Caption size.
140 dc.SetBrush(wx.Brush(color))
141 gcdc.DrawRectangle(rect.x, rect.y, 200+3, rect.height)
142 gcdc.SetPen(wx.TRANSPARENT_PEN)
143 gcdc.DrawRectangle(rect.x+1, rect.y+12, 200, rect.height-12)
144 rect.Inflate(-5, 0)
145 gcdc.SetFont(font)
146 gcdc.DrawText(self._caption, rect.x+3, rect.y+3)
147
148 #---------------------------------------------------------------------------
149
150 class MyDialog(wx.Dialog):
151 """
152 ...
153 """
154 def __init__(self, parent, title,
155 pos=wx.DefaultPosition,
156 size=wx.DefaultSize):
157 wx.Dialog.__init__(self,
158 parent,
159 -1,
160 title,
161 pos=(-1, -1),
162 size=(420, 550),
163 style=wx.DEFAULT_FRAME_STYLE)
164
165 #------------
166
167 # Attributes.
168 self.parent = parent
169
170 #------------
171
172 # Simplified init method.
173 self.SetProperties()
174 self.CreateCtrls()
175 self.DoLayout()
176
177 #------------
178
179 self.CenterOnScreen(wx.BOTH)
180 print("\nDisplay the caption box dialog")
181
182 #------------
183
184 self.ShowModal()
185 self.Destroy()
186
187 #-----------------------------------------------------------------------
188
189 def SetProperties(self):
190 """
191 ...
192 """
193
194 frameicon = wx.Icon("icon_wxWidgets.ico")
195 self.SetIcon(frameicon)
196
197 self.SetMinSize((420, 550))
198
199
200 def CreateCtrls(self):
201 """
202 ...
203 """
204
205 self.firstBox = MyCaptionBox(self, "First caption")
206
207 # Create some static text in the box.
208 for x in range(4):
209 st = wx.StaticText(self.firstBox,
210 label="- blah, blah, blah, blah... %d" % x)
211 self.firstBox.AddItem(st)
212
213 #------------
214
215 self.secondBox = MyCaptionBox(self, "Second caption")
216
217 # Create some static text in the box.
218 for x in range(4):
219 st = wx.StaticText(self.secondBox,
220 label="- blah, blah, blah, blah... %d" % x)
221 self.secondBox.AddItem(st)
222
223 #------------
224
225 self.thirdBox = MyCaptionBox(self, "Third caption")
226
227 # Create some static text in the box.
228 for x in range(4):
229 st = wx.StaticText(self.thirdBox,
230 label="- blah, blah, blah, blah... %d" % x)
231 self.thirdBox.AddItem(st)
232
233 #------------
234
235 self.btnSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
236
237
238 def DoLayout(self):
239 """
240 ...
241 """
242
243 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
244 ctrlSizer = wx.BoxSizer(wx.VERTICAL)
245
246 ctrlSizer.Add(self.firstBox, 1, wx.EXPAND)
247 ctrlSizer.Add(self.secondBox, 1, wx.EXPAND)
248 ctrlSizer.Add(self.thirdBox, 1, wx.EXPAND)
249 ctrlSizer.Add(self.btnSizer, 0, wx.EXPAND |
250 wx.RIGHT, 5)
251
252 mainSizer.Add(ctrlSizer, 1, wx.EXPAND |
253 wx.BOTTOM, 10)
254
255 self.SetSizer(mainSizer)
256 mainSizer.Layout()
257
258 #---------------------------------------------------------------------------
259
260 class MyFrame(wx.Frame):
261 """
262 ...
263 """
264 def __init__(self):
265 super(MyFrame, self).__init__(None,
266 -1,
267 title="")
268
269 #------------
270
271 # Return application name.
272 self.app_name = wx.GetApp().GetAppName()
273
274 #------------
275
276 # Simplified init method.
277 self.SetProperties()
278 self.CreateCtrls()
279 self.BindEvents()
280 self.DoLayout()
281
282 #------------
283
284 self.CenterOnScreen()
285
286 #-----------------------------------------------------------------------
287
288 def SetProperties(self):
289 """
290 ...
291 """
292
293 self.SetTitle(self.app_name)
294
295 #------------
296
297 frameicon = wx.Icon("icon_wxWidgets.ico")
298 self.SetIcon(frameicon)
299
300
301 def CreateCtrls(self):
302 """
303 ...
304 """
305
306 # Create a panel.
307 self.panel = wx.Panel(self, -1)
308
309 #------------
310
311 # Add some buttons.
312 self.btnDlg = wx.Button(self.panel,
313 -1,
314 "&Show caption box dialog")
315
316 self.btnClose = wx.Button(self.panel,
317 -1,
318 "&Close")
319
320
321 def BindEvents(self):
322 """
323 Bind some events to an events handler.
324 """
325
326 # Bind the close event to an event handler.
327 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
328
329 # Bind the buttons event to an event handler.
330 self.Bind(wx.EVT_BUTTON, self.OnSetting, self.btnDlg)
331 self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
332
333
334 def DoLayout(self):
335 """
336 ...
337 """
338
339 # MainSizer is the top-level one that manages everything.
340 mainSizer = wx.BoxSizer(wx.VERTICAL)
341
342 # wx.BoxSizer(window, proportion, flag, border)
343 # wx.BoxSizer(sizer, proportion, flag, border)
344 mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10)
345 mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
346
347 # Finally, tell the panel to use the sizer for layout.
348 self.panel.SetAutoLayout(True)
349 self.panel.SetSizer(mainSizer)
350
351 mainSizer.Fit(self.panel)
352
353 #-----------------------------------------------------------------------
354
355 def OnCloseMe(self, event):
356 """
357 ...
358 """
359
360 self.Close(True)
361
362
363 def OnSetting(self, event):
364 """
365 ...
366 """
367
368 settingDlg = MyDialog(self,
369 title="Caption box dialog")
370 #pos=(-1, -1),
371 #size=(500, 300))
372
373 def OnCloseWindow(self, event):
374 """
375 ...
376 """
377
378 self.Destroy()
379
380 #---------------------------------------------------------------------------
381
382 class MyApp(wx.App):
383 """
384 ...
385 """
386 def OnInit(self):
387
388 #------------
389
390 self.SetAppName("Main frame")
391
392 #------------
393
394 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
395
396 #------------
397
398 frame = MyFrame()
399 self.SetTopWindow(frame)
400 frame.Show(True)
401
402 return True
403
404 #-----------------------------------------------------------------------
405
406 def GetInstallDir(self):
407 """
408 Returns the installation directory for my application.
409 """
410
411 return self.installDir
412
413 #---------------------------------------------------------------------------
414
415 def main():
416 app = MyApp(False)
417 app.MainLoop()
418
419 #---------------------------------------------------------------------------
420
421 if __name__ == "__main__" :
422 main()
Download source
Additional Information
Link :
- - - - -
https://wiki.wxpython.org/TitleIndex
Thanks to
Cody Precord, Robin Dunn, the wxPython community...
About this page
Date (d/m/y) Person (bot) Comments :
22/04/18 - Ecco (Created page for wxPython Phoenix).
Comments
- blah, blah, blah....