Customized splash screen (Phoenix)

Keywords : Splashscreen, Mask, Timeout, Shaped frame, Bitmap, Timer, Gauge, Throbber, Animated gif, CallAfter, CallLater, ScreenDC.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


With gauge :

First example

img_sample_one_a.png

   1 # sample_one_a.py
   2 
   3 import sys
   4 import os
   5 import wx
   6 from   wx.adv import SplashScreen as SplashScreen
   7 
   8 # class MyFrame
   9 # class MySplash
  10 # class MyApp
  11 
  12 #---------------------------------------------------------------------------
  13 
  14 class MyFrame(wx.Frame):
  15     """
  16     ...
  17     """
  18     def __init__(self):
  19         super(MyFrame, self).__init__(None,
  20                                       -1,
  21                                       title="")
  22 
  23         #------------
  24 
  25         # Return application name.
  26         self.app_name = wx.GetApp().GetAppName()
  27         # Return icons folder.
  28         self.icons_dir = wx.GetApp().GetIconsDir()
  29 
  30         #------------
  31 
  32         # Simplified init method.
  33         self.SetProperties()
  34         self.CreateCtrls()
  35         self.BindEvents()
  36         self.DoLayout()
  37 
  38         #------------
  39 
  40         self.CenterOnScreen(wx.BOTH)
  41 
  42         #------------
  43 
  44         self.Show(True)
  45 
  46     #-----------------------------------------------------------------------
  47 
  48     def SetProperties(self):
  49         """
  50         ...
  51         """
  52 
  53         self.SetTitle(self.app_name)
  54         self.SetSize((340, 250))
  55 
  56         #------------
  57 
  58         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  59                                          "icon_wxWidgets.ico"),
  60                             type=wx.BITMAP_TYPE_ICO)
  61         self.SetIcon(frameIcon)
  62 
  63 
  64     def CreateCtrls(self):
  65         """
  66         ...
  67         """
  68 
  69         # Create a panel.
  70         self.panel = wx.Panel(self, -1)
  71 
  72         #------------
  73 
  74         # Add a button.
  75         self.btnClose = wx.Button(self.panel,
  76                                   -1,
  77                                   "&Close")
  78 
  79 
  80     def BindEvents(self):
  81         """
  82         Bind some events to an event handler.
  83         """
  84 
  85         # Bind events to an events handler.
  86         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  87         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
  88 
  89 
  90     def DoLayout(self):
  91         """
  92         ...
  93         """
  94 
  95         # MainSizer is the top-level one that manages everything.
  96         mainSizer = wx.BoxSizer(wx.VERTICAL)
  97 
  98         # wx.BoxSizer(window, proportion, flag, border).
  99         # wx.BoxSizer(sizer, proportion, flag, border).
 100         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 101 
 102         # Finally, tell the panel to use the sizer for layout.
 103         self.panel.SetAutoLayout(True)
 104         self.panel.SetSizer(mainSizer)
 105 
 106         mainSizer.Fit(self.panel)
 107 
 108     #-----------------------------------------------------------------------
 109 
 110     def OnCloseMe(self, event):
 111         """
 112         ...
 113         """
 114 
 115         self.Close(True)
 116 
 117 
 118     def OnCloseWindow(self, event):
 119         """
 120         ...
 121         """
 122 
 123         self.Destroy()
 124 
 125 #---------------------------------------------------------------------------
 126 
 127 class MySplash(SplashScreen):
 128     """
 129     ...
 130     """
 131     def __init__(self):
 132 
 133         #--------------
 134 
 135         screen = wx.ScreenDC()
 136 
 137         # Screen size.
 138         ws, hs = screen.GetSize()
 139 
 140         #--------------
 141 
 142         # Return bitmaps folder.
 143         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 144 
 145         # Load a background bitmap.
 146         bitmap = wx.Bitmap(os.path.join(self.bitmaps_dir,
 147                                         "python.png"),
 148                            type=wx.BITMAP_TYPE_PNG)
 149 
 150         # Determine size of bitmap.
 151         wi, hi = bitmap.GetWidth(), bitmap.GetHeight()
 152         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 153 
 154         x = (ws-wi)/2
 155         y = (hs-hi)/2
 156 
 157         #--------------
 158 
 159         super(MySplash, self).__init__(bitmap=bitmap,
 160                                        splashStyle=wx.adv.SPLASH_CENTRE_ON_SCREEN |
 161                                                    wx.adv.SPLASH_TIMEOUT,
 162                                        milliseconds=8000,
 163                                        parent=None,
 164                                        id=-1,
 165                                        pos=(x, y),
 166                                        size=(wi, hi),
 167                                        style=wx.STAY_ON_TOP |
 168                                              wx.BORDER_NONE)
 169 
 170         #------------
 171 
 172         self.SetBackgroundColour("#f0f0f0")
 173 
 174         self.count = 0
 175 
 176         wx.Sleep(1)
 177 
 178         #------------
 179 
 180         # Simplified init method.
 181         self.CreateCtrls()
 182         self.BindEvents()
 183 
 184         #------------
 185 
 186         # Create a timer for my gauge.
 187         self.timer = wx.Timer(self)
 188 
 189         # Gauge speed. Simulate long startup time.
 190         self.timer.Start(90)
 191 
 192         self.Bind(wx.EVT_TIMER, self.TimerHandler, self.timer)
 193 
 194         #------------
 195 
 196         print("\n... Display the splashScreen")
 197 
 198         #--------------
 199 
 200         wx.BeginBusyCursor()
 201 
 202     #-----------------------------------------------------------------------
 203 
 204     def CreateCtrls(self):
 205         """
 206         Create some controls for my splash screen.
 207         """
 208 
 209         #------------
 210 
 211         # Put text.
 212         self.text = wx.StaticText(parent=self,
 213                                   id=-1,
 214                                   label="Loading...",
 215                                   pos=(4, 231),
 216                                   size=(252, 18),
 217                                   style=wx.ALIGN_CENTRE_HORIZONTAL)
 218         self.text.SetBackgroundColour(wx.WHITE)
 219 
 220         #------------
 221 
 222         # Put gauge.
 223         self.gauge = wx.Gauge(self,
 224                               id=-1,
 225                               range=50,
 226                               size=(-1, 20))
 227 
 228         #------------
 229 
 230         # Cody Precord... thank you !
 231         rect = self.GetClientRect()
 232         new_size = (rect.width, 20)
 233         self.gauge.SetSize(new_size)
 234         self.SetSize((rect.width, rect.height+20))
 235         self.gauge.SetPosition((0, rect.height))
 236 
 237 
 238     def BindEvents(self):
 239         """
 240         Bind all the events related to my splash screen.
 241         """
 242 
 243         # Bind events to an events handler.
 244         self.Bind(wx.EVT_CLOSE, self.OnClose)
 245 
 246 
 247     def TimerHandler(self, event):
 248         """
 249         ...
 250         """
 251 
 252         self.count = self.count + 1
 253 
 254         if self.count >= 90:
 255             self.count = 0
 256 
 257         self.gauge.SetValue(self.count)
 258         # or
 259         # self.gauge.Pulse()
 260 
 261 
 262     def OnClose(self, event):
 263         """
 264         Close the splash screen.
 265         This method will be called under 2 cases :
 266         1. time-limit is up, called automatically,
 267         2. you left-click on the splash-bitmap.
 268         """
 269 
 270         # Make sure the default handler runs
 271         # too so this window gets destroyed.
 272         # Tell the event system to continue
 273         # looking for an event handler, so the
 274         # default handler will get called.
 275         event.Skip()
 276         self.Hide()
 277 
 278         #------------
 279 
 280         if self.timer.IsRunning():
 281             # Stop the gauge timer.
 282             self.timer.Stop()
 283             # del self.timer
 284             self.ShowMainFrame()
 285 
 286 
 287     def ShowMainFrame(self):
 288         """
 289         ...
 290         """
 291 
 292         print("\n... Close the splash screen")
 293         print("\n... Create and display the main frame")
 294 
 295         #------------
 296 
 297         wx.CallAfter(wx.EndBusyCursor)
 298 
 299         #------------
 300 
 301         # Create an instance of the MyFrame class.
 302         frame = MyFrame()
 303 
 304 #---------------------------------------------------------------------------
 305 
 306 class MyApp(wx.App):
 307     """
 308     ...
 309     """
 310     def OnInit(self):
 311 
 312         #------------
 313 
 314         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 315 
 316         #------------
 317 
 318         self.SetAppName("Main frame")
 319 
 320         #------------
 321 
 322         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 323 
 324         #------------
 325 
 326         splash = MySplash()
 327         splash.Show(True)
 328 
 329         return True
 330 
 331     #-----------------------------------------------------------------------
 332 
 333     def GetInstallDir(self):
 334         """
 335         Returns the installation directory for my application.
 336         """
 337 
 338         return self.installDir
 339 
 340 
 341     def GetIconsDir(self):
 342         """
 343         Returns the icons directory for my application.
 344         """
 345 
 346         icons_dir = os.path.join(self.installDir, "icons")
 347         return icons_dir
 348 
 349 
 350     def GetBitmapsDir(self):
 351         """
 352         Returns the bitmaps directory for my application.
 353         """
 354 
 355         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 356         return bitmaps_dir
 357 
 358 #---------------------------------------------------------------------------
 359 
 360 def main():
 361     app = MyApp(False)
 362     app.MainLoop()
 363 
 364 #---------------------------------------------------------------------------
 365 
 366 if __name__ == "__main__" :
 367     main()


Second example

img_sample_one_b.png

   1 # sample_one_b.py
   2 
   3 import os
   4 import sys
   5 import platform
   6 import wx
   7 
   8 # class MyFrame
   9 # class MyCaptionBox
  10 # class MyPanel
  11 # class MySplash
  12 # class MyApp
  13 
  14 #---------------------------------------------------------------------------
  15 
  16 class MyFrame(wx.Frame):
  17     """
  18     ...
  19     """
  20     def __init__(self):
  21         super(MyFrame, self).__init__(None,
  22                                       -1,
  23                                       title="")
  24 
  25         #------------
  26 
  27         # Return application name.
  28         self.app_name = wx.GetApp().GetAppName()
  29         # Return icons folder.
  30         self.icons_dir = wx.GetApp().GetIconsDir()
  31 
  32         #------------
  33 
  34         # Simplified init method.
  35         self.SetProperties()
  36         self.CreateCtrls()
  37         self.BindEvents()
  38         self.DoLayout()
  39 
  40         #------------
  41 
  42         self.CenterOnScreen(wx.BOTH)
  43 
  44         #------------
  45 
  46         self.Show(True)
  47 
  48     #-----------------------------------------------------------------------
  49 
  50     def SetProperties(self):
  51         """
  52         ...
  53         """
  54 
  55         self.SetTitle(self.app_name)
  56         self.SetSize((340, 250))
  57 
  58         #------------
  59 
  60         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  61                                          "icon_wxWidgets.ico"),
  62                             type=wx.BITMAP_TYPE_ICO)
  63         self.SetIcon(frameIcon)
  64 
  65 
  66     def CreateCtrls(self):
  67         """
  68         ...
  69         """
  70 
  71         # Create a panel.
  72         self.panel = wx.Panel(self, -1)
  73 
  74         #------------
  75 
  76         # Add a buttons.
  77         self.btnClose = wx.Button(self.panel,
  78                                   -1,
  79                                   "&Close")
  80 
  81 
  82     def BindEvents(self):
  83         """
  84         Bind some events to an events handler.
  85         """
  86 
  87         # Bind events to an events handler.
  88         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  89         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
  90 
  91 
  92     def DoLayout(self):
  93         """
  94         ...
  95         """
  96 
  97         # MainSizer is the top-level one that manages everything.
  98         mainSizer = wx.BoxSizer(wx.VERTICAL)
  99 
 100         # wx.BoxSizer(window, proportion, flag, border)
 101         # wx.BoxSizer(sizer, proportion, flag, border)
 102         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 103 
 104         # Finally, tell the panel to use the sizer for layout.
 105         self.panel.SetAutoLayout(True)
 106         self.panel.SetSizer(mainSizer)
 107 
 108         mainSizer.Fit(self.panel)
 109 
 110     #-----------------------------------------------------------------------
 111 
 112     def OnCloseMe(self, event):
 113         """
 114         ...
 115         """
 116 
 117         self.Close(True)
 118 
 119 
 120     def OnCloseWindow(self, event):
 121         """
 122         ...
 123         """
 124 
 125         self.Destroy()
 126         sys.exit()
 127 
 128 #---------------------------------------------------------------------------
 129 
 130 class MyCaptionBox(wx.Panel):
 131     """
 132     ...
 133     """
 134     def __init__(self, parent, caption):
 135         super(MyCaptionBox, self).__init__(parent,
 136                                            style=wx.NO_BORDER |
 137                                                  wx.TAB_TRAVERSAL)
 138 
 139         #------------
 140 
 141         self.SetBackgroundColour("#344050")
 142 
 143         #------------
 144 
 145         # Attributes.
 146         self._caption = caption
 147         self._csizer = wx.BoxSizer(wx.VERTICAL)
 148 
 149         #------------
 150 
 151         # Simplified init method.
 152         self.BindEvents()
 153         self.DoLayout()
 154 
 155     #-----------------------------------------------------------------------
 156 
 157     def BindEvents(self):
 158         """
 159         ...
 160         """
 161 
 162         # Bind events to an events handler.
 163         self.Bind(wx.EVT_PAINT, self.OnPaint)
 164 
 165 
 166     def DoLayout(self):
 167         """
 168         ...
 169         """
 170 
 171         msizer = wx.BoxSizer(wx.HORIZONTAL)
 172         self._csizer.AddSpacer(15) # Extra space for caption.
 173         msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 0)
 174         self.SetSizer(msizer)
 175 
 176 
 177     def DoGetBestSize(self):
 178         """
 179         ...
 180         """
 181 
 182         size = super(MyCaptionBox, self).DoGetBestSize()
 183         # Compensate for wide caption labels.
 184         tw = self.GetTextExtent(self._caption)[0]
 185         size.SetWidth(max(size.width, tw))
 186         return size
 187 
 188 
 189     def AddItem(self, item):
 190         """
 191         Add a window or sizer item to the caption box.
 192         """
 193 
 194         self._csizer.Add(item, 0, wx.ALL, 5)
 195 
 196 
 197     def OnPaint(self, event):
 198         """
 199         Draws the Caption and border around the controls.
 200         """
 201 
 202         dc = wx.PaintDC(self)
 203         dc.SetBackground(wx.Brush(wx.BLACK))
 204         dc.Clear()
 205         gcdc = wx.GCDC(dc)
 206         dc.Clear()
 207 
 208         # Get the working rectangle we can draw in.
 209         rect = self.GetClientRect()
 210 
 211         # Get the sytem color to draw the caption.
 212         ss = wx.SystemSettings
 213         color = ss.GetColour(wx.SYS_COLOUR_3DDKSHADOW)
 214         gcdc.SetTextForeground(wx.WHITE)
 215 
 216         # Draw the border.
 217         rect.Inflate(-0, -0)
 218         gcdc.SetPen(wx.Pen(color))
 219         gcdc.SetBrush(wx.Brush(wx.Colour(70, 70, 70, 255)))
 220         gcdc.DrawRoundedRectangle(rect, 5)
 221 
 222         # Font size and style.
 223         font = self.GetFont().GetPointSize()
 224         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
 225         font.SetWeight(wx.BOLD)
 226 
 227         # Add the Caption.
 228         rect = wx.Rect(rect.x, rect.y, rect.width, 20)
 229         rect.Inflate(-5, 0)
 230         gcdc.SetFont(font)
 231         gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
 232 
 233 #---------------------------------------------------------------------------
 234 
 235 class MyPanel(wx.Panel):
 236     """
 237     ...
 238     """
 239     def __init__(self, parent):
 240         wx.Panel.__init__(self, parent)
 241 
 242         #------------
 243 
 244         self.SetBackgroundColour("#344050")
 245 
 246         #------------
 247 
 248         # Return bitmaps folder.
 249         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 250 
 251         # Load a background bitmap.
 252         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 253                                           "rocket.png"),
 254                              type=wx.BITMAP_TYPE_PNG)
 255 
 256         #------------
 257 
 258         # Attributes.
 259         self.box1 = MyCaptionBox(self, "Loading...")
 260 
 261         #------------
 262 
 263         # Put a gauge in the box.
 264         self.count = 0
 265 
 266         self.gauge = wx.Gauge(self.box1,
 267                               id=-1,
 268                               range=20,
 269                               size=(155, 15))
 270         self.box1.AddItem(self.gauge)
 271 
 272         #------------
 273 
 274         # Create a timer for my gauge.
 275         self.timer = wx.Timer(self)
 276 
 277         # Gauge speed. Simulate long startup time.
 278         self.timer.Start(90)
 279 
 280         self.Bind(wx.EVT_TIMER, self.TimerHandler, self.timer)
 281 
 282         #------------
 283 
 284         # Simplified init method.
 285         self.BindEvents()
 286         self.DoLayout()
 287 
 288         #------------
 289 
 290         self.SetClientSize((514, 386))
 291 
 292     #-----------------------------------------------------------------------
 293 
 294     def BindEvents(self):
 295         """
 296         ...
 297         """
 298 
 299         # Bind event to an events handler.
 300         self.Bind(wx.EVT_PAINT, self.OnPaint)
 301         self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
 302         self.box1.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 303         self.gauge.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 304 
 305 
 306     def DoLayout(self):
 307         """
 308         ...
 309         """
 310 
 311         hsizer = wx.BoxSizer(wx.HORIZONTAL)
 312         vsizer = wx.BoxSizer(wx.VERTICAL)
 313 
 314         #------------
 315 
 316         # Add the box to the panel.
 317         hsizer.AddStretchSpacer()
 318         hsizer.Add(self.box1, 0, wx.EXPAND)
 319         hsizer.AddSpacer(0)
 320         vsizer.AddStretchSpacer()
 321         vsizer.Add(hsizer, 0, wx.EXPAND|wx.ALL, 15)
 322 
 323         self.SetSizer(vsizer)
 324 
 325 
 326     def OnPaint(self, event):
 327         """
 328         ...
 329         """
 330 
 331         dc = wx.BufferedPaintDC(self)
 332         dc.Clear()
 333         gcdc = wx.GCDC(dc)
 334         gcdc.Clear()
 335 
 336         # Draw a bitmap with an alpha channel
 337         # on top of the last group.
 338         gcdc.DrawBitmap(self.bmp, 0, 0, False)
 339 
 340 
 341     def TimerHandler(self, event):
 342         """
 343         ...
 344         """
 345 
 346         self.count = self.count + 1
 347 
 348         if self.count >= 90:
 349             self.count = 0
 350 
 351         self.gauge.SetValue(self.count)
 352         # or
 353         # self.gauge.Pulse()
 354 
 355 
 356     def OnMouseEvents(self, event):
 357         """
 358         Handles the wx.EVT_MOUSE_EVENTS for AdvancedSplash.
 359         This reproduces the behavior of wx.SplashScreen.
 360         """
 361 
 362         if event.LeftDown() or event.RightDown():
 363             splash = wx.GetApp().GetTopWindow()
 364             splash.OnClose(event)
 365 
 366         event.Skip()
 367 
 368 
 369     def OnDestroy(self, event):
 370         """
 371         ...
 372         """
 373 
 374         event.Skip()
 375 
 376 #---------------------------------------------------------------------------
 377 
 378 class MySplash(wx.Frame):
 379     """
 380     ...
 381     """
 382     def __init__(self):
 383 
 384         #--------------
 385 
 386         screen = wx.ScreenDC()
 387 
 388         # Screen size.
 389         ws, hs = screen.GetSize()
 390 
 391         #--------------
 392 
 393         # Return bitmaps folder.
 394         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 395 
 396         # Load a background bitmap.
 397         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 398                                           "rocket.png"),
 399                              type=wx.BITMAP_TYPE_PNG)
 400 
 401         mask = wx.Mask(self.bmp, wx.RED)
 402         self.bmp.SetMask(mask)
 403 
 404         # Determine size of bitmap.
 405         wi, hi = self.bmp.GetWidth(), self.bmp.GetHeight()
 406         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 407 
 408         x = (ws-wi)/2
 409         y = (hs-hi)/2
 410 
 411         #--------------
 412 
 413         super(MySplash, self).__init__(parent=None,
 414                                        id=-1,
 415                                        title="SplashScreen",
 416                                        pos=(x, y),
 417                                        size=(wi, hi),
 418                                        style=wx.FRAME_SHAPED |
 419                                              wx.BORDER_NONE |
 420                                              wx.FRAME_NO_TASKBAR |
 421                                              wx.STAY_ON_TOP)
 422 
 423         #--------------
 424 
 425         self.SetBackgroundColour("#344050")
 426 
 427         #--------------
 428 
 429         # Attributes.
 430         self.hasShape = False
 431 
 432         #--------------
 433 
 434         if wx.Platform != "__WXMAC__":
 435             # wxMac clips the tooltip to the window shape, YUCK!!!
 436             self.SetToolTip("Right-click to close the window\n"
 437                             "Double-click the image to set/unset the window shape")
 438 
 439         if wx.Platform == "__WXGTK__":
 440             # wxGTK requires that the window be created before you can
 441             # set its shape, so delay the call to SetWindowShape until
 442             # this event.
 443             self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
 444         else:
 445             # On wxMSW and wxMac the window has already
 446             # been created, so go for it.
 447             self.SetWindowShape()
 448 
 449         #--------------
 450 
 451         # Starts the Timer. Once Expired, splash is Destroyed.
 452         self.timer = wx.Timer(self)
 453 
 454         # Simulate long startup time.
 455         self.timer.Start(4000)
 456 
 457         self.Bind(wx.EVT_TIMER, self.TimeOut, self.timer)
 458 
 459         #--------------
 460 
 461         # Show main frame after 3000 ms.
 462         self.fc = wx.CallLater(3000, self.ShowMainFrame)
 463 
 464         #--------------
 465 
 466         self.CenterOnScreen(wx.BOTH)
 467         self.Show(True)
 468 
 469         #--------------
 470 
 471         # Simplified init method.
 472         self.CreateCtrls()
 473         self.DoLayout()
 474 
 475         #--------------
 476 
 477         print("\n... Display the splashScreen")
 478 
 479         #--------------
 480 
 481         self.SetClientSize((wi, hi))
 482         self.SetTransparent(255)
 483         wx.BeginBusyCursor()
 484 
 485     #-----------------------------------------------------------------------
 486 
 487     def CreateCtrls(self):
 488         """
 489         ...
 490         """
 491 
 492         self.panel = MyPanel(self)
 493 
 494 
 495     def BindEvents(self):
 496         """
 497         Bind all the events related to my app.
 498         """
 499 
 500         # Bind some events to an events handler.
 501         self.Bind(wx.EVT_CHAR, self.OnCharEvents)
 502         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 503         self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnClose)
 504 
 505 
 506     def DoLayout(self):
 507         """
 508         ...
 509         """
 510 
 511         sizer = wx.BoxSizer(wx.VERTICAL)
 512         sizer.Add(self.panel, 1, wx.EXPAND)
 513         self.SetSizer(sizer)
 514 
 515 
 516     def SetWindowShape(self, *evt):
 517         """
 518         ...
 519         """
 520 
 521         # Use the bitmap's mask to determine the region.
 522         r = wx.Region(self.bmp)
 523         self.hasShape = self.SetShape(r)
 524 
 525 
 526     def OnCharEvents(self, event):
 527         """
 528         Handles the wx.EVT_CHAR for Splash.
 529         This reproduces the behavior of wx.SplashScreen.
 530         """
 531 
 532         self.OnClose(event)
 533 
 534 
 535     def TimeOut(self, event):
 536         """
 537         ...
 538         """
 539 
 540         self.Close(True)
 541 
 542 
 543     def OnCloseWindow(self, event):
 544         """
 545         ...
 546         """
 547 
 548         print("\n... Close timer")
 549 
 550         #--------------
 551 
 552         if hasattr(self, "timer"):
 553             self.Show(False)
 554             self.timer.Stop()
 555             del self.timer
 556 
 557 
 558     def OnClose(self, event):
 559         """
 560         Handles the wx.EVT_CLOSE event for SplashScreen.
 561         """
 562 
 563         # Make sure the default handler runs
 564         # too so this window gets destroyed.
 565         # Tell the event system to continue
 566         # looking for an event handler, so the
 567         # default handler will get called.
 568         event.Skip()
 569         self.Hide()
 570 
 571         #------------
 572 
 573         # If the timer is still running then go
 574         # ahead and show the main frame now.
 575         if self.fc.IsRunning():
 576             # Stop the wx.CallLater timer.
 577             # Stop the splash screen timer
 578             # and close it.
 579             self.fc.Stop()
 580             self.ShowMainFrame()
 581 
 582 
 583     def ShowMainFrame(self):
 584         """
 585         ...
 586         """
 587 
 588         print("\n... Close the splash screen")
 589         print("\n... Create and display the main frame")
 590 
 591         #------------
 592 
 593         wx.CallAfter(wx.EndBusyCursor)
 594 
 595         #------------
 596 
 597         if self.fc.IsRunning():
 598             # Stop the splash screen
 599             # timer and close it.
 600             self.Raise()
 601 
 602         #------------
 603 
 604         # Create an instance of the MyFrame class.
 605         frame = MyFrame()
 606 
 607 #---------------------------------------------------------------------------
 608 
 609 class MyApp(wx.App):
 610     """
 611     ...
 612     """
 613     def OnInit(self):
 614 
 615         #------------
 616 
 617         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 618 
 619         #------------
 620 
 621         self.SetAppName("Main frame")
 622 
 623         #------------
 624 
 625         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 626 
 627         #------------
 628 
 629         splash = MySplash()
 630         splash.BindEvents()
 631         splash.CenterOnScreen(wx.BOTH)
 632 
 633         return True
 634 
 635     #-----------------------------------------------------------------------
 636 
 637     def GetInstallDir(self):
 638         """
 639         Returns the installation directory for my application.
 640         """
 641 
 642         return self.installDir
 643 
 644 
 645     def GetIconsDir(self):
 646         """
 647         Returns the icons directory for my application.
 648         """
 649 
 650         icons_dir = os.path.join(self.installDir, "icons")
 651         return icons_dir
 652 
 653 
 654     def GetBitmapsDir(self):
 655         """
 656         Returns the bitmaps directory for my application.
 657         """
 658 
 659         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 660         return bitmaps_dir
 661 
 662 #---------------------------------------------------------------------------
 663 
 664 def main():
 665     app = MyApp(False)
 666     app.MainLoop()
 667 
 668 #---------------------------------------------------------------------------
 669 
 670 if __name__ == "__main__" :
 671     main()


With shadow

img_sample_two.png

   1 # sample_two.py
   2 
   3 import sys
   4 import os
   5 import wx
   6 from   wx.adv import SplashScreen as SplashScreen
   7 
   8 # class MyFrame
   9 # class MySplash
  10 # class MyApp
  11 
  12 #---------------------------------------------------------------------------
  13 
  14 class MyFrame(wx.Frame):
  15     """
  16     ...
  17     """
  18     def __init__(self):
  19         super(MyFrame, self).__init__(None,
  20                                       -1,
  21                                       title="")
  22 
  23         #------------
  24 
  25         # Return application name.
  26         self.app_name = wx.GetApp().GetAppName()
  27         # Return icons folder.
  28         self.icons_dir = wx.GetApp().GetIconsDir()
  29 
  30         #------------
  31 
  32         # Simplified init method.
  33         self.SetProperties()
  34         self.CreateCtrls()
  35         self.BindEvents()
  36         self.DoLayout()
  37 
  38         #------------
  39 
  40         self.CenterOnScreen(wx.BOTH)
  41 
  42         #------------
  43 
  44         self.Show(True)
  45 
  46     #-----------------------------------------------------------------------
  47 
  48     def SetProperties(self):
  49         """
  50         ...
  51         """
  52 
  53         self.SetTitle(self.app_name)
  54         self.SetSize((340, 250))
  55 
  56         #------------
  57 
  58         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  59                                          "icon_wxWidgets.ico"),
  60                             type=wx.BITMAP_TYPE_ICO)
  61         self.SetIcon(frameIcon)
  62 
  63 
  64     def CreateCtrls(self):
  65         """
  66         ...
  67         """
  68 
  69         # Create a panel.
  70         self.panel = wx.Panel(self, -1)
  71 
  72         #------------
  73 
  74         # Add a button.
  75         self.btnClose = wx.Button(self.panel,
  76                                   -1,
  77                                   "&Close")
  78 
  79 
  80     def BindEvents(self):
  81         """
  82         Bind some events to an events handler.
  83         """
  84 
  85         # Bind events to an events handler.
  86         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  87         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
  88 
  89 
  90     def DoLayout(self):
  91         """
  92         ...
  93         """
  94 
  95         # MainSizer is the top-level one that manages everything.
  96         mainSizer = wx.BoxSizer(wx.VERTICAL)
  97 
  98         # wx.BoxSizer(window, proportion, flag, border)
  99         # wx.BoxSizer(sizer, proportion, flag, border)
 100         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 101 
 102         # Finally, tell the panel to use the sizer for layout.
 103         self.panel.SetAutoLayout(True)
 104         self.panel.SetSizer(mainSizer)
 105 
 106         mainSizer.Fit(self.panel)
 107 
 108     #-----------------------------------------------------------------------
 109 
 110     def OnCloseMe(self, event):
 111         """
 112         ...
 113         """
 114 
 115         self.Close(True)
 116 
 117 
 118     def OnCloseWindow(self, event):
 119         """
 120         ...
 121         """
 122 
 123         self.Destroy()
 124 
 125 #---------------------------------------------------------------------------
 126 
 127 class MySplash(SplashScreen):
 128     """
 129     ...
 130     """
 131     def __init__(self):
 132 
 133         self.SaveScreen()
 134 
 135         #------------
 136 
 137         # Return bitmaps folder.
 138         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 139 
 140         #------------
 141 
 142         # Load a background bitmap.
 143         bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 144                                      "splashscreen.png"),
 145                         type=wx.BITMAP_TYPE_PNG)
 146 
 147         # Determine size of bitmap.
 148         imgSize = (bmp.GetWidth(), bmp.GetHeight())
 149         print("\n... Bitmap size : %sx%s px" % (imgSize))
 150 
 151         #------------
 152 
 153         super(MySplash, self).__init__(bitmap=bmp,
 154                                        splashStyle=wx.adv.SPLASH_TIMEOUT,
 155                                        milliseconds=4000,
 156                                        parent=None,
 157                                        id=-1,
 158                                        pos=wx.DefaultPosition,
 159                                        size=imgSize,
 160                                        style=wx.STAY_ON_TOP |
 161                                              wx.BORDER_NONE)
 162 
 163         #------------
 164 
 165         # Simplified init method.
 166         self.BindEvents()
 167 
 168         #------------
 169 
 170         print("\n... Display the splashScreen")
 171 
 172         #--------------
 173 
 174         wx.BeginBusyCursor()
 175 
 176     #-----------------------------------------------------------------------
 177 
 178     def BindEvents(self):
 179         """
 180         ...
 181         """
 182 
 183         # Bind events to an events handler.
 184         self.Bind(wx.EVT_CLOSE, self.OnClose)
 185 
 186 
 187     def SaveScreen(self):
 188         """
 189         ...
 190         """
 191 
 192         # Create a screenshot.
 193         screen = wx.ScreenDC()
 194         # Screen size.
 195         w, h = screen.GetSize()
 196         print("\n... Screen size : %sx%s px" % (w, h))
 197         bmp = wx.Bitmap(w, h)
 198         mem = wx.MemoryDC(screen)
 199         mem.SelectObject(bmp)
 200         mem.Blit(0, 0, w, h, screen, 0, 0)
 201 
 202         #------------
 203 
 204         # Return bitmaps folder.
 205         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 206 
 207         # Load a bitmap.
 208         bmp2 = wx.Bitmap(os.path.join(self.bitmaps_dir,
 209                                       "shadow.png"),
 210                          type=wx.BITMAP_TYPE_PNG)
 211 
 212         # Bitmap size.
 213         w2, h2 = bmp2.GetSize()
 214 
 215         # Draw a bitmap with an alpha
 216         # channel on the screenshot.
 217         # image, x, y, transparency.
 218         mem.DrawBitmap(bmp2,(w-w2)/2, (h-h2)/2, False)
 219 
 220         mem.SelectObject(wx.NullBitmap)
 221 
 222         #------------
 223 
 224         # Save a background bitmap for splashscreen.
 225         bmp.SaveFile(os.path.join(self.bitmaps_dir,
 226                                   "splashscreen.png"),
 227                      type=wx.BITMAP_TYPE_PNG)
 228 
 229 
 230     def OnClose(self, event):
 231         """
 232         Close the splash screen.
 233         This method will be called under 2 cases :
 234         1. time-limit is up, called automatically,
 235         2. you left-click on the splash-bitmap.
 236         """
 237 
 238         # Make sure the default handler runs
 239         # too so this window gets destroyed.
 240         # Tell the event system to continue
 241         # looking for an event handler, so the
 242         # default handler will get called.
 243         event.Skip()
 244         self.Hide()
 245 
 246         #------------
 247 
 248         self.ShowMainFrame()
 249 
 250 
 251     def ShowMainFrame(self):
 252         """
 253         ...
 254         """
 255 
 256         print("\n... Close the splash screen")
 257         print("\n... Create and display the main frame")
 258 
 259         #------------
 260 
 261         wx.CallAfter(wx.EndBusyCursor)
 262 
 263         #------------
 264 
 265         # Create an instance of the MyFrame class.
 266         frame = MyFrame()
 267 
 268 #---------------------------------------------------------------------------
 269 
 270 class MyApp(wx.App):
 271     """
 272     ...
 273     """
 274     def OnInit(self):
 275 
 276         #------------
 277 
 278         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 279 
 280         #------------
 281 
 282         self.SetAppName("Main frame")
 283 
 284         #------------
 285 
 286         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 287 
 288         #------------
 289 
 290         splash = MySplash()
 291         splash.Show(True)
 292 
 293         return True
 294 
 295     #-----------------------------------------------------------------------
 296 
 297     def GetInstallDir(self):
 298         """
 299         Returns the installation directory for my application.
 300         """
 301 
 302         return self.installDir
 303 
 304 
 305     def GetIconsDir(self):
 306         """
 307         Returns the icons directory for my application.
 308         """
 309 
 310         icons_dir = os.path.join(self.installDir, "icons")
 311         return icons_dir
 312 
 313 
 314     def GetBitmapsDir(self):
 315         """
 316         Returns the bitmaps directory for my application.
 317         """
 318 
 319         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 320         return bitmaps_dir
 321 
 322 #---------------------------------------------------------------------------
 323 
 324 def main():
 325     app = MyApp(False)
 326     app.MainLoop()
 327 
 328 #---------------------------------------------------------------------------
 329 
 330 if __name__ == "__main__" :
 331     main()


With throbber

img_sample_three.png

THROBBER file : throb_images.py

   1 # sample_three.py
   2 
   3 import os
   4 import sys
   5 import wx
   6 import wx.lib.throbber as throb
   7 import throb_images
   8 
   9 # class MyFrame
  10 # class MyCaptionBox
  11 # class MyPanel
  12 # class MySplash
  13 # class MyApp
  14 
  15 #---------------------------------------------------------------------------
  16 
  17 class MyFrame(wx.Frame):
  18     """
  19     ...
  20     """
  21     def __init__(self):
  22         super(MyFrame, self).__init__(None,
  23                                       -1,
  24                                       title="")
  25 
  26         #------------
  27 
  28         # Return application name.
  29         self.app_name = wx.GetApp().GetAppName()
  30         # Return icons folder.
  31         self.icons_dir = wx.GetApp().GetIconsDir()
  32 
  33         #------------
  34 
  35         # Simplified init method.
  36         self.SetProperties()
  37         self.CreateCtrls()
  38         self.BindEvents()
  39         self.DoLayout()
  40 
  41         #------------
  42 
  43         self.CenterOnScreen(wx.BOTH)
  44 
  45         #------------
  46 
  47         self.Show(True)
  48 
  49     #-----------------------------------------------------------------------
  50 
  51     def SetProperties(self):
  52         """
  53         ...
  54         """
  55 
  56         self.SetTitle(self.app_name)
  57         self.SetSize((340, 250))
  58 
  59         #------------
  60 
  61         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  62                                          "icon_wxWidgets.ico"),
  63                             type=wx.BITMAP_TYPE_ICO)
  64         self.SetIcon(frameIcon)
  65 
  66 
  67     def CreateCtrls(self):
  68         """
  69         ...
  70         """
  71 
  72         # Create a panel.
  73         self.panel = wx.Panel(self, -1)
  74 
  75         #------------
  76 
  77         # Add a buttons.
  78         self.btnClose = wx.Button(self.panel,
  79                                   -1,
  80                                   "&Close")
  81 
  82 
  83     def BindEvents(self):
  84         """
  85         Bind some events to an events handler.
  86         """
  87 
  88         # Bind events to an events handler.
  89         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  90         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
  91 
  92 
  93     def DoLayout(self):
  94         """
  95         ...
  96         """
  97 
  98         # MainSizer is the top-level one that manages everything.
  99         mainSizer = wx.BoxSizer(wx.VERTICAL)
 100 
 101         # wx.BoxSizer(window, proportion, flag, border)
 102         # wx.BoxSizer(sizer, proportion, flag, border)
 103         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 104 
 105         # Finally, tell the panel to use the sizer for layout.
 106         self.panel.SetAutoLayout(True)
 107         self.panel.SetSizer(mainSizer)
 108 
 109         mainSizer.Fit(self.panel)
 110 
 111     #-----------------------------------------------------------------------
 112 
 113     def OnCloseMe(self, event):
 114         """
 115         ...
 116         """
 117 
 118         self.Close(True)
 119 
 120 
 121     def OnCloseWindow(self, event):
 122         """
 123         ...
 124         """
 125 
 126         self.Destroy()
 127         sys.exit()
 128 
 129 #---------------------------------------------------------------------------
 130 
 131 class MyCaptionBox(wx.Panel):
 132     """
 133     ...
 134     """
 135     def __init__(self, parent, caption):
 136         super(MyCaptionBox, self).__init__(parent,
 137                                            style=wx.NO_BORDER |
 138                                                  wx.TAB_TRAVERSAL)
 139 
 140         #------------
 141 
 142         self.SetBackgroundColour("#344050")
 143 
 144         #------------
 145 
 146         # Attributes.
 147         self._caption = caption
 148         self._csizer = wx.BoxSizer(wx.VERTICAL)
 149 
 150         #------------
 151 
 152         # Simplified init method.
 153         self.BindEvents()
 154         self.DoLayout()
 155 
 156     #-----------------------------------------------------------------------
 157 
 158     def BindEvents(self):
 159         """
 160         ...
 161         """
 162 
 163         # Bind events to an events handler.
 164         self.Bind(wx.EVT_PAINT, self.OnPaint)
 165 
 166 
 167     def DoLayout(self):
 168         """
 169         ...
 170         """
 171 
 172         msizer = wx.BoxSizer(wx.HORIZONTAL)
 173         self._csizer.AddSpacer(15) # Extra space for caption.
 174         msizer.Add(self._csizer, 0, wx.EXPAND)
 175         self.SetSizer(msizer)
 176 
 177 
 178     def DoGetBestSize(self):
 179         """
 180         ...
 181         """
 182 
 183         size = super(MyCaptionBox, self).DoGetBestSize()
 184         # Compensate for wide caption labels.
 185         tw = self.GetTextExtent(self._caption)[0]
 186         size.SetWidth(max(size.width, tw))
 187         return size
 188 
 189 
 190     def AddItem(self, item):
 191         """
 192         Add a window or sizer item to the caption box.
 193         """
 194 
 195         self._csizer.Add(item, 0, wx.ALL, 10)
 196 
 197 
 198     def OnPaint(self, event):
 199         """
 200         Draws the Caption and border around the controls.
 201         """
 202 
 203         dc = wx.PaintDC(self)
 204         dc.SetBackground(wx.Brush(wx.Colour(52, 64, 80, 255)))
 205         dc.Clear()
 206         gcdc = wx.GCDC(dc)
 207         dc.Clear()
 208 
 209         # Get the working rectangle we can draw in.
 210         rect = self.GetClientRect()
 211 
 212         # Get the sytem color to draw the caption.
 213         ss = wx.SystemSettings
 214         color = ss.GetColour(wx.SYS_COLOUR_3DDKSHADOW)
 215         gcdc.SetTextForeground(wx.WHITE)
 216 
 217         # Draw the border.
 218         rect.Inflate(-0, -0)
 219         gcdc.SetPen(wx.Pen(color))
 220         gcdc.SetBrush(wx.Brush(wx.Colour(52, 64, 80, 255)))
 221 
 222         # Font size and style.
 223         font = self.GetFont().GetPointSize()
 224         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
 225         font.SetWeight(wx.BOLD)
 226 
 227         # Add the Caption.
 228         rect = wx.Rect(rect.x, rect.y, rect.width, 20)
 229         rect.Inflate(-5, 0)
 230         gcdc.SetFont(font)
 231         gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
 232 
 233 #---------------------------------------------------------------------------
 234 
 235 class MyPanel(wx.Panel):
 236     """
 237     ...
 238     """
 239     def __init__(self, parent):
 240         wx.Panel.__init__(self, parent)
 241 
 242         #------------
 243 
 244         self.SetBackgroundColour("#4a6991")
 245 
 246         #------------
 247 
 248         # Return bitmaps folder.
 249         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 250 
 251         # Load a background bitmap.
 252         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 253                                           "throbber.png"),
 254                              type=wx.BITMAP_TYPE_PNG)
 255 
 256         #------------
 257 
 258         # Attributes.
 259         self.box1 = MyCaptionBox(self, "Loading...")
 260 
 261         #------------
 262 
 263         # Throbber image list.
 264         self.images = [throb_images.catalog[img].GetBitmap()
 265                        for img in throb_images.index
 266                        if img not in ["eclouds", "logo"]]
 267 
 268 
 269         # Put a throbber in the box.
 270         self.throb = throb.Throbber(self.box1,
 271                                     -1,
 272                                     self.images,
 273                                     frameDelay=0.1,
 274                                     rest=4)
 275         self.box1.AddItem(self.throb)
 276 
 277         self.throb.Start()
 278 
 279         #------------
 280 
 281         # Simplified init method.
 282         self.BindEvents()
 283         self.DoLayout()
 284 
 285         #------------
 286 
 287         self.SetClientSize((262, 262))
 288 
 289     #-----------------------------------------------------------------------
 290 
 291     def BindEvents(self):
 292         """
 293         ...
 294         """
 295 
 296         # Bind some events to an events handler.
 297         self.Bind(wx.EVT_PAINT, self.OnPaint)
 298         self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
 299         self.box1.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 300         self.throb.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 301 
 302 
 303     def DoLayout(self):
 304         """
 305         ...
 306         """
 307 
 308         hsizer = wx.BoxSizer(wx.HORIZONTAL)
 309         vsizer = wx.BoxSizer(wx.VERTICAL)
 310 
 311         #------------
 312 
 313         # Add the box to the panel.
 314         hsizer.AddStretchSpacer()
 315         hsizer.Add(self.box1, 0, wx.ALIGN_CENTER)
 316         vsizer.AddStretchSpacer(40)
 317         vsizer.Add(hsizer, 0, wx.ALIGN_CENTER)
 318         vsizer.AddStretchSpacer()
 319 
 320         self.SetSizer(vsizer)
 321 
 322 
 323     def OnPaint(self, event):
 324         """
 325         ...
 326         """
 327 
 328         dc = wx.BufferedPaintDC(self)
 329         dc.Clear()
 330         gcdc = wx.GCDC(dc)
 331         gcdc.Clear()
 332 
 333         # Draw a bitmap with an alpha channel
 334         # on top of the last group.
 335         gcdc.DrawBitmap(self.bmp, 0, 0, False)
 336 
 337 
 338     def OnMouseEvents(self, event):
 339         """
 340         Handles the wx.EVT_MOUSE_EVENTS for AdvancedSplash.
 341         This reproduces the behavior of wx.SplashScreen.
 342         """
 343 
 344         if event.LeftDown() or event.RightDown():
 345             splash = wx.GetApp().GetTopWindow()
 346             splash.OnClose(event)
 347 
 348         event.Skip()
 349 
 350 
 351     def OnDestroy(self, event):
 352         """
 353         ...
 354         """
 355 
 356         self.throb.Stop()
 357         event.Skip()
 358 
 359 #---------------------------------------------------------------------------
 360 
 361 class MySplash(wx.Frame):
 362     """
 363     ...
 364     """
 365     def __init__(self):
 366 
 367         #--------------
 368 
 369         screen = wx.ScreenDC()
 370 
 371         # Screen size.
 372         ws, hs = screen.GetSize()
 373 
 374         #--------------
 375 
 376         # Return bitmaps folder.
 377         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 378 
 379         # Load a background bitmap.
 380         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 381                                           "throbber.png"),
 382                              type=wx.BITMAP_TYPE_PNG)
 383 
 384         mask = wx.Mask(self.bmp, wx.RED)
 385         self.bmp.SetMask(mask)
 386 
 387         # Determine size of bitmap.
 388         wi, hi = self.bmp.GetWidth(), self.bmp.GetHeight()
 389         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 390 
 391         x = (ws-wi)/2
 392         y = (hs-hi)/2
 393 
 394         #--------------
 395 
 396         super(MySplash, self).__init__(parent=None,
 397                                        id=-1,
 398                                        title="SplashScreen",
 399                                        pos=(x, y),
 400                                        size=(wi, hi),
 401                                        style=wx.FRAME_SHAPED |
 402                                              wx.BORDER_NONE |
 403                                              wx.FRAME_NO_TASKBAR |
 404                                              wx.STAY_ON_TOP)
 405 
 406         #--------------
 407 
 408         self.SetBackgroundColour("#4a6991")
 409 
 410         #--------------
 411 
 412         # Attributes.
 413         self.hasShape = False
 414 
 415         #--------------
 416 
 417         if wx.Platform != "__WXMAC__":
 418             # wxMac clips the tooltip to the window shape, YUCK!!!
 419             self.SetToolTip("Right-click to close the window\n"
 420                             "Double-click the image to set/unset the window shape")
 421 
 422         if wx.Platform == "__WXGTK__":
 423             # wxGTK requires that the window be created before you can
 424             # set its shape, so delay the call to SetWindowShape until
 425             # this event.
 426             self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
 427         else:
 428             # On wxMSW and wxMac the window has already
 429             # been created, so go for it.
 430             self.SetWindowShape()
 431 
 432         #--------------
 433 
 434         # Starts the Timer. Once Expired, splash is Destroyed.
 435         self.timer = wx.Timer(self)
 436 
 437         # Simulate long startup time.
 438         self.timer.Start(4000)
 439 
 440         self.Bind(wx.EVT_TIMER, self.TimeOut, self.timer)
 441 
 442         #--------------
 443 
 444         # Show main frame after 3000 ms.
 445         self.fc = wx.CallLater(3000, self.ShowMainFrame)
 446 
 447         #--------------
 448 
 449         self.CenterOnScreen(wx.BOTH)
 450         self.Show(True)
 451 
 452         #--------------
 453 
 454         # Simplified init method.
 455         self.CreateCtrls()
 456         self.DoLayout()
 457 
 458         #--------------
 459 
 460         print("\n... Display the splashScreen")
 461 
 462         #--------------
 463 
 464         self.SetClientSize((wi, hi))
 465         self.SetTransparent(255)
 466         wx.BeginBusyCursor()
 467 
 468     #-----------------------------------------------------------------------
 469 
 470 
 471     def CreateCtrls(self):
 472         """
 473         ...
 474         """
 475 
 476 
 477         self.panel = MyPanel(self)
 478 
 479 
 480     def BindEvents(self):
 481         """
 482         Bind all the events related to my app.
 483         """
 484 
 485         # Bind some events to an events handler.
 486         self.Bind(wx.EVT_CHAR, self.OnCharEvents)
 487         self.Bind(wx.EVT_CLOSE, self.OnClose)
 488         self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnClose)
 489 
 490 
 491     def DoLayout(self):
 492         """
 493         ...
 494         """
 495 
 496         sizer = wx.BoxSizer(wx.VERTICAL)
 497         sizer.Add(self.panel, 1, wx.EXPAND, 20)
 498         self.SetSizer(sizer)
 499 
 500 
 501     def SetWindowShape(self, *evt):
 502         """
 503         ...
 504         """
 505 
 506         # Use the bitmap's mask to determine the region.
 507         r = wx.Region(self.bmp)
 508         self.hasShape = self.SetShape(r)
 509 
 510 
 511     def OnCharEvents(self, event):
 512         """
 513         Handles the wx.EVT_CHAR for Splash.
 514         This reproduces the behavior of wx.SplashScreen.
 515         """
 516 
 517         self.OnClose(event)
 518 
 519 
 520     def TimeOut(self, event):
 521         """
 522         ...
 523         """
 524 
 525         self.Close(True)
 526 
 527 
 528     def OnClose(self, event):
 529         """
 530         Handles the wx.EVT_CLOSE event for SplashScreen.
 531         """
 532 
 533         # Make sure the default handler runs
 534         # too so this window gets destroyed.
 535         # Tell the event system to continue
 536         # looking for an event handler, so the
 537         # default handler will get called.
 538         # event.Skip()  # ?????
 539         self.Hide()
 540 
 541         #------------
 542 
 543         # If the timer is still running then go
 544         # ahead and show the main frame now.
 545         if self.fc.IsRunning():
 546             # Stop the wx.CallLater timer.
 547             # Stop the splash screen timer
 548             # and close it.
 549             self.fc.Stop()
 550             self.ShowMainFrame()
 551 
 552 
 553     def ShowMainFrame(self):
 554         """
 555         ...
 556         """
 557 
 558         print("\n... Close the splash screen")
 559         print("\n... Create and display the main frame")
 560 
 561         #------------
 562 
 563         wx.CallAfter(wx.EndBusyCursor)
 564 
 565         #------------
 566 
 567         if self.fc.IsRunning():
 568             # Stop the splash screen
 569             # timer and close it.
 570             self.Raise()
 571 
 572         #------------
 573 
 574         # Create an instance of the MyFrame class.
 575         frame = MyFrame()
 576 
 577 #---------------------------------------------------------------------------
 578 
 579 class MyApp(wx.App):
 580     """
 581     ...
 582     """
 583     def OnInit(self):
 584 
 585         #------------
 586 
 587         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 588 
 589         #------------
 590 
 591         self.SetAppName("Main frame")
 592 
 593         #------------
 594 
 595         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 596 
 597         #------------
 598 
 599         splash = MySplash()
 600         splash.BindEvents()
 601         splash.CenterOnScreen(wx.BOTH)
 602 
 603         return True
 604 
 605     #-----------------------------------------------------------------------
 606 
 607     def GetInstallDir(self):
 608         """
 609         Returns the installation directory for my application.
 610         """
 611 
 612         return self.installDir
 613 
 614 
 615     def GetIconsDir(self):
 616         """
 617         Returns the icons directory for my application.
 618         """
 619 
 620         icons_dir = os.path.join(self.installDir, "icons")
 621         return icons_dir
 622 
 623 
 624     def GetBitmapsDir(self):
 625         """
 626         Returns the bitmaps directory for my application.
 627         """
 628 
 629         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 630         return bitmaps_dir
 631 
 632 #---------------------------------------------------------------------------
 633 
 634 def main():
 635     app = MyApp(False)
 636     app.MainLoop()
 637 
 638 #---------------------------------------------------------------------------
 639 
 640 if __name__ == "__main__" :
 641     main()


With animated gif

img_sample_four.png

   1 # sample_four.py
   2 
   3 import os
   4 import sys
   5 import wx
   6 from wx.adv import Animation, AnimationCtrl
   7 
   8 # class MyFrame
   9 # class MyCaptionBox
  10 # class MyPanel
  11 # class MySplash
  12 # class MyApp
  13 
  14 #---------------------------------------------------------------------------
  15 
  16 class MyFrame(wx.Frame):
  17     """
  18     ...
  19     """
  20     def __init__(self):
  21         super(MyFrame, self).__init__(None,
  22                                       -1,
  23                                       title="")
  24 
  25         #------------
  26 
  27         # Return application name.
  28         self.app_name = wx.GetApp().GetAppName()
  29         # Return icons folder.
  30         self.icons_dir = wx.GetApp().GetIconsDir()
  31 
  32         #------------
  33 
  34         # Simplified init method.
  35         self.SetProperties()
  36         self.CreateCtrls()
  37         self.BindEvents()
  38         self.DoLayout()
  39 
  40         #------------
  41 
  42         self.CenterOnScreen(wx.BOTH)
  43 
  44         #------------
  45 
  46         self.Show(True)
  47 
  48     #-----------------------------------------------------------------------
  49 
  50     def SetProperties(self):
  51         """
  52         ...
  53         """
  54 
  55         self.SetTitle(self.app_name)
  56         self.SetSize((340, 250))
  57 
  58         #------------
  59 
  60         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  61                                          "icon_wxWidgets.ico"),
  62                             type=wx.BITMAP_TYPE_ICO)
  63         self.SetIcon(frameIcon)
  64 
  65 
  66     def CreateCtrls(self):
  67         """
  68         ...
  69         """
  70 
  71         # Create a panel.
  72         self.panel = wx.Panel(self, -1)
  73 
  74         #------------
  75 
  76         # Add a buttons.
  77         self.btnClose = wx.Button(self.panel,
  78                                   -1,
  79                                   "&Close")
  80 
  81 
  82     def BindEvents(self):
  83         """
  84         Bind some events to an events handler.
  85         """
  86 
  87         # Bind events to an events handler.
  88         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  89         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
  90 
  91 
  92     def DoLayout(self):
  93         """
  94         ...
  95         """
  96 
  97         # MainSizer is the top-level one that manages everything.
  98         mainSizer = wx.BoxSizer(wx.VERTICAL)
  99 
 100         # wx.BoxSizer(window, proportion, flag, border)
 101         # wx.BoxSizer(sizer, proportion, flag, border)
 102         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 103 
 104         # Finally, tell the panel to use the sizer for layout.
 105         self.panel.SetAutoLayout(True)
 106         self.panel.SetSizer(mainSizer)
 107 
 108         mainSizer.Fit(self.panel)
 109 
 110     #-----------------------------------------------------------------------
 111 
 112     def OnCloseMe(self, event):
 113         """
 114         ...
 115         """
 116 
 117         self.Close(True)
 118 
 119 
 120     def OnCloseWindow(self, event):
 121         """
 122         ...
 123         """
 124 
 125         self.Destroy()
 126         sys.exit()
 127 
 128 #---------------------------------------------------------------------------
 129 
 130 class MyCaptionBox(wx.Panel):
 131     """
 132     ...
 133     """
 134     def __init__(self, parent, caption):
 135         super(MyCaptionBox, self).__init__(parent,
 136                                            style=wx.NO_BORDER |
 137                                                  wx.TAB_TRAVERSAL)
 138 
 139         #------------
 140 
 141         self.SetBackgroundColour("gray")
 142 
 143         #------------
 144 
 145         # Attributes.
 146         self._caption = caption
 147         self._csizer = wx.BoxSizer(wx.VERTICAL)
 148 
 149         #------------
 150 
 151         # Simplified init method.
 152         self.BindEvents()
 153         self.DoLayout()
 154 
 155     #-----------------------------------------------------------------------
 156 
 157     def BindEvents(self):
 158         """
 159         ...
 160         """
 161 
 162         # Bind events to an events handler.
 163         self.Bind(wx.EVT_PAINT, self.OnPaint)
 164 
 165 
 166     def DoLayout(self):
 167         """
 168         ...
 169         """
 170 
 171         msizer = wx.BoxSizer(wx.HORIZONTAL)
 172         self._csizer.AddSpacer(15) # Extra space for caption.
 173         msizer.Add(self._csizer, 0, wx.EXPAND)
 174         self.SetSizer(msizer)
 175 
 176 
 177     def DoGetBestSize(self):
 178         """
 179         ...
 180         """
 181 
 182         size = super(MyCaptionBox, self).DoGetBestSize()
 183         # Compensate for wide caption labels.
 184         tw = self.GetTextExtent(self._caption)[0]
 185         size.SetWidth(max(size.width, tw))
 186         return size
 187 
 188 
 189     def AddItem(self, item):
 190         """
 191         Add a window or sizer item to the caption box.
 192         """
 193 
 194         self._csizer.Add(item, 0, wx.ALL, 10)
 195 
 196 
 197     def OnPaint(self, event):
 198         """
 199         Draws the Caption and border around the controls.
 200         """
 201 
 202         dc = wx.PaintDC(self)
 203         dc.SetBackground(wx.Brush(wx.Colour(255, 255, 255, 255)))
 204         dc.Clear()
 205         gcdc = wx.GCDC(dc)
 206         dc.Clear()
 207 
 208         # Get the working rectangle we can draw in.
 209         rect = self.GetClientRect()
 210 
 211         # Get the sytem color to draw the caption.
 212         ss = wx.SystemSettings
 213         color = ss.GetColour(wx.SYS_COLOUR_3DDKSHADOW)
 214         gcdc.SetTextForeground(wx.BLACK)
 215 
 216         # Draw the border.
 217         rect.Inflate(-0, -0)
 218         gcdc.SetPen(wx.Pen(color))
 219         gcdc.SetBrush(wx.Brush(wx.Colour(52, 64, 80, 255)))
 220 
 221         # Font size and style.
 222         font = self.GetFont().GetPointSize()
 223         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
 224         font.SetWeight(wx.BOLD)
 225 
 226         # Add the Caption.
 227         rect = wx.Rect(rect.x, rect.y, rect.width, 20)
 228         rect.Inflate(-5, 0)
 229         gcdc.SetFont(font)
 230         gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
 231 
 232 #---------------------------------------------------------------------------
 233 
 234 class MyPanel(wx.Panel):
 235     """
 236     ...
 237     """
 238     def __init__(self, parent):
 239         wx.Panel.__init__(self, parent)
 240 
 241         #------------
 242 
 243         self.SetBackgroundColour("gray")
 244 
 245         #------------
 246 
 247         # Return bitmaps folder.
 248         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 249 
 250         # Load a background bitmap.
 251         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 252                                           "gloss.png"),
 253                              type=wx.BITMAP_TYPE_PNG)
 254 
 255         #------------
 256 
 257         # Attributes.
 258         self.box1 = MyCaptionBox(self, "Loading...")
 259 
 260         #------------
 261 
 262         # Pick the filename of an animated GIF file you have ...
 263         # Give it the full path and file name.
 264         animatedGif = Animation(os.path.join(self.bitmaps_dir,
 265                                              "loader.gif"))
 266         self.ag = AnimationCtrl(self.box1,
 267                                 -1,
 268                                 animatedGif)
 269 
 270         # Clears the background.
 271         self.ag.SetBackgroundColour(self.GetBackgroundColour())
 272 
 273         # Continuously loop through the frames of
 274         # the gif file (default).
 275         self.ag.Play()
 276 
 277         self.box1.AddItem(self.ag)
 278 
 279         #------------
 280 
 281         # Simplified init method.
 282         self.BindEvents()
 283         self.DoLayout()
 284 
 285         #------------
 286 
 287         self.SetClientSize((262, 282))
 288 
 289     #-----------------------------------------------------------------------
 290 
 291     def BindEvents(self):
 292         """
 293         ...
 294         """
 295 
 296         # Bind some events to an events handler.
 297         self.Bind(wx.EVT_PAINT, self.OnPaint)
 298         self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
 299         self.ag.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 300 
 301 
 302     def DoLayout(self):
 303         """
 304         ...
 305         """
 306 
 307         hsizer = wx.BoxSizer(wx.HORIZONTAL)
 308         vsizer = wx.BoxSizer(wx.VERTICAL)
 309 
 310         #------------
 311 
 312         # Add the box to the panel.
 313         hsizer.AddStretchSpacer()
 314         hsizer.Add(self.box1, 0, wx.ALIGN_CENTER)
 315         vsizer.AddStretchSpacer(40)
 316         vsizer.Add(hsizer, 0, wx.ALIGN_CENTER)
 317         vsizer.AddStretchSpacer()
 318 
 319         self.SetSizer(vsizer)
 320 
 321 
 322     def OnPaint(self, event):
 323         """
 324         ...
 325         """
 326 
 327         dc = wx.BufferedPaintDC(self)
 328         dc.Clear()
 329         gcdc = wx.GCDC(dc)
 330         gcdc.Clear()
 331 
 332         # Draw a bitmap with an alpha channel
 333         # on top of the last group.
 334         gcdc.DrawBitmap(self.bmp, 0, 0, False)
 335 
 336 
 337     def OnMouseEvents(self, event):
 338         """
 339         Handles the wx.EVT_MOUSE_EVENTS for AdvancedSplash.
 340         This reproduces the behavior of wx.SplashScreen.
 341         """
 342 
 343         if event.LeftDown() or event.RightDown():
 344             splash = wx.GetApp().GetTopWindow()
 345             splash.OnClose(event)
 346 
 347         event.Skip()
 348 
 349 
 350     def OnDestroy(self, event):
 351         """
 352         ...
 353         """
 354 
 355         event.Skip()
 356 
 357 #---------------------------------------------------------------------------
 358 
 359 class MySplash(wx.Frame):
 360     """
 361     ...
 362     """
 363     def __init__(self):
 364 
 365         #--------------
 366 
 367         screen = wx.ScreenDC()
 368 
 369         # Screen size.
 370         ws, hs = screen.GetSize()
 371 
 372         #--------------
 373 
 374         # Return bitmaps folder.
 375         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 376 
 377         # Load a background bitmap.
 378         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 379                                           "gloss.png"),
 380                              type=wx.BITMAP_TYPE_PNG)
 381 
 382         mask = wx.Mask(self.bmp, wx.RED)
 383         self.bmp.SetMask(mask)
 384 
 385         # Determine size of bitmap.
 386         wi, hi = self.bmp.GetWidth(), self.bmp.GetHeight()
 387         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 388 
 389         x = (ws-wi)/2
 390         y = (hs-hi)/2
 391 
 392         #--------------
 393 
 394         super(MySplash, self).__init__(parent=None,
 395                                        id=-1,
 396                                        title="SplashScreen",
 397                                        pos=(x, y),
 398                                        size=(wi, hi),
 399                                        style=wx.FRAME_SHAPED |
 400                                              wx.BORDER_NONE |
 401                                              wx.FRAME_NO_TASKBAR |
 402                                              wx.STAY_ON_TOP)
 403 
 404         #--------------
 405 
 406         self.SetBackgroundColour("gray")
 407 
 408         #--------------
 409 
 410         # Attributes.
 411         self.hasShape = False
 412 
 413         #--------------
 414 
 415         if wx.Platform != "__WXMAC__":
 416             # wxMac clips the tooltip to the window shape, YUCK!!!
 417             self.SetToolTip("Right-click to close the window\n"
 418                             "Double-click the image to set/unset the window shape")
 419 
 420         if wx.Platform == "__WXGTK__":
 421             # wxGTK requires that the window be created before you can
 422             # set its shape, so delay the call to SetWindowShape until
 423             # this event.
 424             self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
 425         else:
 426             # On wxMSW and wxMac the window has already
 427             # been created, so go for it.
 428             self.SetWindowShape()
 429 
 430         #--------------
 431 
 432         # Starts the Timer. Once Expired, splash is Destroyed.
 433         self.timer = wx.Timer(self)
 434 
 435         # Simulate long startup time.
 436         self.timer.Start(4000)
 437 
 438         self.Bind(wx.EVT_TIMER, self.TimeOut, self.timer)
 439 
 440         #--------------
 441 
 442         # Show main frame after 3000 ms.
 443         self.fc = wx.CallLater(3000, self.ShowMainFrame)
 444 
 445         #--------------
 446 
 447         self.CenterOnScreen(wx.BOTH)
 448         self.Show(True)
 449 
 450         #--------------
 451 
 452         # Simplified init method.
 453         self.CreateCtrls()
 454         self.DoLayout()
 455 
 456         #--------------
 457 
 458         print("\n... Display the splashScreen")
 459 
 460         #--------------
 461 
 462         self.SetClientSize((wi, hi))
 463         self.SetTransparent(255)
 464         wx.BeginBusyCursor()
 465 
 466     #-----------------------------------------------------------------------
 467 
 468 
 469     def CreateCtrls(self):
 470         """
 471         ...
 472         """
 473 
 474 
 475         self.panel = MyPanel(self)
 476 
 477 
 478     def BindEvents(self):
 479         """
 480         Bind all the events related to my app.
 481         """
 482 
 483         # Bind some events to an events handler.
 484         self.Bind(wx.EVT_CHAR, self.OnCharEvents)
 485         self.Bind(wx.EVT_CLOSE, self.OnClose)
 486         self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnClose)
 487 
 488 
 489     def DoLayout(self):
 490         """
 491         ...
 492         """
 493 
 494         sizer = wx.BoxSizer(wx.VERTICAL)
 495         sizer.Add(self.panel, 1, wx.EXPAND, 20)
 496         self.SetSizer(sizer)
 497 
 498 
 499     def SetWindowShape(self, *evt):
 500         """
 501         ...
 502         """
 503 
 504         # Use the bitmap's mask to determine the region.
 505         r = wx.Region(self.bmp)
 506         self.hasShape = self.SetShape(r)
 507 
 508 
 509     def OnCharEvents(self, event):
 510         """
 511         Handles the wx.EVT_CHAR for Splash.
 512         This reproduces the behavior of wx.SplashScreen.
 513         """
 514 
 515         self.OnClose(event)
 516 
 517 
 518     def TimeOut(self, event):
 519         """
 520         ...
 521         """
 522 
 523         self.Close(True)
 524 
 525 
 526     def OnClose(self, event):
 527         """
 528         Handles the wx.EVT_CLOSE event for SplashScreen.
 529         """
 530 
 531         # Make sure the default handler runs
 532         # too so this window gets destroyed.
 533         # Tell the event system to continue
 534         # looking for an event handler, so the
 535         # default handler will get called.
 536         # event.Skip()  # ?????
 537         self.Hide()
 538 
 539         #------------
 540 
 541         # If the timer is still running then go
 542         # ahead and show the main frame now.
 543         if self.fc.IsRunning():
 544             # Stop the wx.CallLater timer.
 545             # Stop the splash screen timer
 546             # and close it.
 547             self.fc.Stop()
 548             self.ShowMainFrame()
 549 
 550 
 551     def ShowMainFrame(self):
 552         """
 553         ...
 554         """
 555 
 556         print("\n... Close the splash screen")
 557         print("\n... Create and display the main frame")
 558 
 559         #------------
 560 
 561         wx.CallAfter(wx.EndBusyCursor)
 562 
 563         #------------
 564 
 565         if self.fc.IsRunning():
 566             # Stop the splash screen
 567             # timer and close it.
 568             self.Raise()
 569 
 570         #------------
 571 
 572         # Create an instance of the MyFrame class.
 573         frame = MyFrame()
 574 
 575 #---------------------------------------------------------------------------
 576 
 577 class MyApp(wx.App):
 578     """
 579     ...
 580     """
 581     def OnInit(self):
 582 
 583         #------------
 584 
 585         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 586 
 587         #------------
 588 
 589         self.SetAppName("Main frame")
 590 
 591         #------------
 592 
 593         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 594 
 595         #------------
 596 
 597         splash = MySplash()
 598         splash.BindEvents()
 599         splash.CenterOnScreen(wx.BOTH)
 600 
 601         return True
 602 
 603     #-----------------------------------------------------------------------
 604 
 605     def GetInstallDir(self):
 606         """
 607         Returns the installation directory for my application.
 608         """
 609 
 610         return self.installDir
 611 
 612 
 613     def GetIconsDir(self):
 614         """
 615         Returns the icons directory for my application.
 616         """
 617 
 618         icons_dir = os.path.join(self.installDir, "icons")
 619         return icons_dir
 620 
 621 
 622     def GetBitmapsDir(self):
 623         """
 624         Returns the bitmaps directory for my application.
 625         """
 626 
 627         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 628         return bitmaps_dir
 629 
 630 #---------------------------------------------------------------------------
 631 
 632 def main():
 633     app = MyApp(False)
 634     app.MainLoop()
 635 
 636 #---------------------------------------------------------------------------
 637 
 638 if __name__ == "__main__" :
 639     main()


With masked area

img_sample_five.png

   1 # sample_five.py
   2 
   3 import os
   4 import sys
   5 import platform
   6 import wx
   7 
   8 # class MyFrame
   9 # class MyPanel
  10 # class MySplash
  11 # class MyApp
  12 
  13 #---------------------------------------------------------------------------
  14 
  15 class MyFrame(wx.Frame):
  16     """
  17     ...
  18     """
  19     def __init__(self):
  20         super(MyFrame, self).__init__(None,
  21                                       -1,
  22                                       title="")
  23 
  24         #------------
  25 
  26         # Return application name.
  27         self.app_name = wx.GetApp().GetAppName()
  28         # Return icons folder.
  29         self.icons_dir = wx.GetApp().GetIconsDir()
  30 
  31         #------------
  32 
  33         # Simplified init method.
  34         self.SetProperties()
  35         self.CreateCtrls()
  36         self.BindEvents()
  37         self.DoLayout()
  38 
  39         #------------
  40 
  41         self.CenterOnScreen(wx.BOTH)
  42 
  43         #------------
  44 
  45         self.Show(True)
  46 
  47     #-----------------------------------------------------------------------
  48 
  49     def SetProperties(self):
  50         """
  51         ...
  52         """
  53 
  54         self.SetTitle(self.app_name)
  55         self.SetSize((340, 250))
  56 
  57         #------------
  58 
  59         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  60                                          "icon_wxWidgets.ico"),
  61                             type=wx.BITMAP_TYPE_ICO)
  62         self.SetIcon(frameIcon)
  63 
  64 
  65     def CreateCtrls(self):
  66         """
  67         ...
  68         """
  69 
  70         # Create a panel.
  71         self.panel = wx.Panel(self, -1)
  72 
  73         #------------
  74 
  75         # Add a buttons.
  76         self.btnClose = wx.Button(self.panel,
  77                                   -1,
  78                                   "&Close")
  79 
  80 
  81     def BindEvents(self):
  82         """
  83         Bind some events to an events handler.
  84         """
  85 
  86         # Bind events to an events handler.
  87         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  88         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
  89 
  90 
  91     def DoLayout(self):
  92         """
  93         ...
  94         """
  95 
  96         # MainSizer is the top-level one that manages everything.
  97         mainSizer = wx.BoxSizer(wx.VERTICAL)
  98 
  99         # wx.BoxSizer(window, proportion, flag, border)
 100         # wx.BoxSizer(sizer, proportion, flag, border)
 101         mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)
 102 
 103         # Finally, tell the panel to use the sizer for layout.
 104         self.panel.SetAutoLayout(True)
 105         self.panel.SetSizer(mainSizer)
 106 
 107         mainSizer.Fit(self.panel)
 108 
 109     #-----------------------------------------------------------------------
 110 
 111     def OnCloseMe(self, event):
 112