How to create a customized splash screen (Phoenix)

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


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     def OnCloseMe(self, event):
 110         """
 111         ...
 112         """
 113 
 114         self.Close(True)
 115 
 116 
 117     def OnCloseWindow(self, event):
 118         """
 119         ...
 120         """
 121 
 122         self.Destroy()
 123 
 124 #---------------------------------------------------------------------------
 125 
 126 class MySplash(SplashScreen):
 127     """
 128     ...
 129     """
 130     def __init__(self):
 131 
 132         #--------------
 133 
 134         screen = wx.ScreenDC()
 135 
 136         # Screen size.
 137         ws, hs = screen.GetSize()
 138 
 139         #--------------
 140 
 141         # Return bitmaps folder.
 142         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 143 
 144         # Load a background bitmap.
 145         bitmap = wx.Bitmap(os.path.join(self.bitmaps_dir,
 146                                         "python.png"),
 147                            type=wx.BITMAP_TYPE_PNG)
 148 
 149         # Determine size of bitmap.
 150         wi, hi = bitmap.GetWidth(), bitmap.GetHeight()
 151         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 152 
 153         x = int((ws-wi)/2)
 154         y = int((hs-hi)/2)
 155 
 156         #--------------
 157 
 158         super(MySplash, self).__init__(bitmap=bitmap,
 159                                        splashStyle=wx.adv.SPLASH_CENTRE_ON_SCREEN |
 160                                                    wx.adv.SPLASH_TIMEOUT,
 161                                        milliseconds=8000,
 162                                        parent=None,
 163                                        id=-1,
 164                                        pos=(x, y),
 165                                        size=(wi, hi),
 166                                        style=wx.STAY_ON_TOP |
 167                                              wx.BORDER_NONE)
 168 
 169         #------------
 170 
 171         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
 172 
 173         self.count = 0
 174 
 175         wx.Sleep(1)
 176 
 177         #------------
 178 
 179         # Simplified init method.
 180         self.CreateCtrls()
 181         self.BindEvents()
 182 
 183         #------------
 184 
 185         # Create a timer for my gauge.
 186         self.timer = wx.Timer(self)
 187 
 188         # Gauge speed. Simulate long startup time.
 189         self.timer.Start(90)
 190 
 191         self.Bind(wx.EVT_TIMER, self.TimerHandler, self.timer)
 192 
 193         #------------
 194 
 195         print("\n... Display the splashScreen")
 196 
 197         #--------------
 198 
 199         wx.BeginBusyCursor()
 200 
 201     #-----------------------------------------------------------------------
 202 
 203     def CreateCtrls(self):
 204         """
 205         Create some controls for my splash screen.
 206         """
 207 
 208         #------------
 209 
 210         # Put text.
 211         self.text = wx.StaticText(parent=self,
 212                                   id=-1,
 213                                   label="Loading...",
 214                                   pos=(4, 231),
 215                                   size=(252, 18),
 216                                   style=wx.ALIGN_CENTRE_HORIZONTAL)
 217         self.text.SetBackgroundColour(wx.WHITE)
 218 
 219         #------------
 220 
 221         # Put gauge.
 222         self.gauge = wx.Gauge(self,
 223                               id=-1,
 224                               range=50,
 225                               size=(-1, 20))
 226 
 227         #------------
 228 
 229         # Cody Precord... thank you !
 230         rect = self.GetClientRect()
 231         new_size = (rect.width, 20)
 232         self.gauge.SetSize(new_size)
 233         self.SetSize((rect.width, rect.height+20))
 234         self.gauge.SetPosition((0, rect.height))
 235 
 236 
 237     def BindEvents(self):
 238         """
 239         Bind all the events related to my splash screen.
 240         """
 241 
 242         # Bind events to an events handler.
 243         self.Bind(wx.EVT_CLOSE, self.OnClose)
 244 
 245 
 246     def TimerHandler(self, event):
 247         """
 248         ...
 249         """
 250 
 251         self.count = self.count + 1
 252 
 253         if self.count >= 90:
 254             self.count = 0
 255 
 256         self.gauge.SetValue(self.count)
 257         # or
 258         # self.gauge.Pulse()
 259 
 260 
 261     def OnClose(self, event):
 262         """
 263         Close the splash screen.
 264         This method will be called under 2 cases :
 265         1. time-limit is up, called automatically,
 266         2. you left-click on the splash-bitmap.
 267         """
 268 
 269         # Make sure the default handler runs
 270         # too so this window gets destroyed.
 271         # Tell the event system to continue
 272         # looking for an event handler, so the
 273         # default handler will get called.
 274         event.Skip()
 275         self.Hide()
 276 
 277         #------------
 278 
 279         if self.timer.IsRunning():
 280             # Stop the gauge timer.
 281             self.timer.Stop()
 282             # del self.timer
 283             self.ShowMainFrame()
 284 
 285 
 286     def ShowMainFrame(self):
 287         """
 288         ...
 289         """
 290 
 291         print("\n... Close the splash screen")
 292         print("\n... Create and display the main frame")
 293 
 294         #------------
 295 
 296         wx.CallAfter(wx.EndBusyCursor)
 297 
 298         #------------
 299 
 300         # Create an instance of the MyFrame class.
 301         frame = MyFrame()
 302 
 303 #---------------------------------------------------------------------------
 304 
 305 class MyApp(wx.App):
 306     """
 307     ...
 308     """
 309     def OnInit(self):
 310 
 311         #------------
 312 
 313         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 314 
 315         #------------
 316 
 317         self.SetAppName("Main frame")
 318 
 319         #------------
 320 
 321         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 322 
 323         #------------
 324 
 325         splash = MySplash()
 326         splash.Show(True)
 327 
 328         return True
 329 
 330     #-----------------------------------------------------------------------
 331 
 332     def GetInstallDir(self):
 333         """
 334         Returns the installation directory for my application.
 335         """
 336 
 337         return self.installDir
 338 
 339 
 340     def GetIconsDir(self):
 341         """
 342         Returns the icons directory for my application.
 343         """
 344 
 345         icons_dir = os.path.join(self.installDir, "icons")
 346         return icons_dir
 347 
 348 
 349     def GetBitmapsDir(self):
 350         """
 351         Returns the bitmaps directory for my application.
 352         """
 353 
 354         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 355         return bitmaps_dir
 356 
 357 #---------------------------------------------------------------------------
 358 
 359 def main():
 360     app = MyApp(False)
 361     app.MainLoop()
 362 
 363 #---------------------------------------------------------------------------
 364 
 365 if __name__ == "__main__" :
 366     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     def OnCloseMe(self, event):
 112         """
 113         ...
 114         """
 115 
 116         self.Close(True)
 117 
 118 
 119     def OnCloseWindow(self, event):
 120         """
 121         ...
 122         """
 123 
 124         self.Destroy()
 125         wx.Exit()
 126 
 127 #---------------------------------------------------------------------------
 128 
 129 class MyCaptionBox(wx.Panel):
 130     """
 131     ...
 132     """
 133     def __init__(self, parent, caption):
 134         super(MyCaptionBox, self).__init__(parent,
 135                                            style=wx.NO_BORDER |
 136                                                  wx.TAB_TRAVERSAL)
 137 
 138         #------------
 139 
 140         self.SetBackgroundColour("#344050")
 141 
 142         #------------
 143 
 144         # Attributes.
 145         self._caption = caption
 146         self._csizer = wx.BoxSizer(wx.VERTICAL)
 147 
 148         #------------
 149 
 150         # Simplified init method.
 151         self.BindEvents()
 152         self.DoLayout()
 153 
 154     #-----------------------------------------------------------------------
 155 
 156     def BindEvents(self):
 157         """
 158         ...
 159         """
 160 
 161         # Bind events to an events handler.
 162         self.Bind(wx.EVT_PAINT, self.OnPaint)
 163 
 164 
 165     def DoLayout(self):
 166         """
 167         ...
 168         """
 169 
 170         msizer = wx.BoxSizer(wx.HORIZONTAL)
 171         self._csizer.AddSpacer(15) # Extra space for caption.
 172         msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 0)
 173         self.SetSizer(msizer)
 174 
 175 
 176     def DoGetBestSize(self):
 177         """
 178         ...
 179         """
 180 
 181         size = super(MyCaptionBox, self).DoGetBestSize()
 182         # Compensate for wide caption labels.
 183         tw = self.GetTextExtent(self._caption)[0]
 184         size.SetWidth(max(size.width, tw))
 185         return size
 186 
 187 
 188     def AddItem(self, item):
 189         """
 190         Add a window or sizer item to the caption box.
 191         """
 192 
 193         self._csizer.Add(item, 0, wx.ALL, 5)
 194 
 195 
 196     def OnPaint(self, event):
 197         """
 198         Draws the Caption and border around the controls.
 199         """
 200 
 201         dc = wx.BufferedPaintDC(self)
 202         dc.SetBackground(wx.Brush(wx.BLACK))
 203         dc.Clear()
 204         gcdc = wx.GCDC(dc)
 205 
 206         # Get the working rectangle we can draw in.
 207         rect = self.GetClientRect()
 208 
 209         # Get the sytem color to draw the caption.
 210         ss = wx.SystemSettings
 211         color = ss.GetColour(wx.SYS_COLOUR_3DDKSHADOW)
 212         gcdc.SetTextForeground(wx.WHITE)
 213 
 214         # Draw the border.
 215         rect.Inflate(-0, -0)
 216         gcdc.SetPen(wx.Pen(color))
 217         gcdc.SetBrush(wx.Brush(wx.Colour(70, 70, 70, 255)))
 218         gcdc.DrawRoundedRectangle(rect, 5)
 219 
 220         # Font size and style.
 221         font = self.GetFont().GetPointSize()
 222         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
 223         font.SetWeight(wx.BOLD)
 224 
 225         # Add the Caption.
 226         rect = wx.Rect(rect.x, rect.y, rect.width, 20)
 227         rect.Inflate(-5, 0)
 228         gcdc.SetFont(font)
 229         gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
 230 
 231 #---------------------------------------------------------------------------
 232 
 233 class MyPanel(wx.Panel):
 234     """
 235     ...
 236     """
 237     def __init__(self, parent):
 238         wx.Panel.__init__(self, parent)
 239 
 240         #------------
 241 
 242         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
 243 
 244         #------------
 245 
 246         # Return bitmaps folder.
 247         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 248 
 249         # Load a background bitmap.
 250         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 251                                           "rocket.png"),
 252                              type=wx.BITMAP_TYPE_PNG)
 253 
 254         #------------
 255 
 256         # Attributes.
 257         self.box1 = MyCaptionBox(self, "Loading...")
 258 
 259         #------------
 260 
 261         # Put a gauge in the box.
 262         self.count = 0
 263 
 264         self.gauge = wx.Gauge(self.box1,
 265                               id=-1,
 266                               range=20,
 267                               size=(155, 15))
 268         self.box1.AddItem(self.gauge)
 269 
 270         #------------
 271 
 272         # Create a timer for my gauge.
 273         self.timer = wx.Timer(self)
 274 
 275         # Gauge speed. Simulate long startup time.
 276         self.timer.Start(90)
 277 
 278         self.Bind(wx.EVT_TIMER, self.TimerHandler, self.timer)
 279 
 280         #------------
 281 
 282         # Simplified init method.
 283         self.BindEvents()
 284         self.DoLayout()
 285 
 286         #------------
 287 
 288         self.SetClientSize((514, 386))
 289 
 290     #-----------------------------------------------------------------------
 291 
 292     def BindEvents(self):
 293         """
 294         ...
 295         """
 296 
 297         # Bind event to an events handler.
 298         self.Bind(wx.EVT_PAINT, self.OnPaint)
 299         self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
 300         self.box1.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 301         self.gauge.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 302 
 303 
 304     def DoLayout(self):
 305         """
 306         ...
 307         """
 308 
 309         hsizer = wx.BoxSizer(wx.HORIZONTAL)
 310         vsizer = wx.BoxSizer(wx.VERTICAL)
 311 
 312         #------------
 313 
 314         # Add the box to the panel.
 315         hsizer.AddStretchSpacer()
 316         hsizer.Add(self.box1, 0, wx.EXPAND)
 317         hsizer.AddSpacer(0)
 318         vsizer.AddStretchSpacer()
 319         vsizer.Add(hsizer, 0, wx.EXPAND|wx.ALL, 15)
 320 
 321         self.SetSizer(vsizer)
 322 
 323 
 324     def OnPaint(self, event):
 325         """
 326         ...
 327         """
 328 
 329         dc = wx.BufferedPaintDC(self)
 330         dc.Clear()
 331         gcdc = wx.GCDC(dc)
 332         gcdc.Clear()
 333 
 334         # Draw a bitmap with an alpha channel
 335         # on top of the last group.
 336         gcdc.DrawBitmap(self.bmp, 0, 0, False)
 337 
 338 
 339     def TimerHandler(self, event):
 340         """
 341         ...
 342         """
 343 
 344         self.count = self.count + 1
 345 
 346         if self.count >= 90:
 347             self.count = 0
 348 
 349         self.gauge.SetValue(self.count)
 350         # or
 351         # self.gauge.Pulse()
 352 
 353 
 354     def OnMouseEvents(self, event):
 355         """
 356         Handles the wx.EVT_MOUSE_EVENTS for AdvancedSplash.
 357         This reproduces the behavior of wx.SplashScreen.
 358         """
 359 
 360         if event.LeftDown() or event.RightDown():
 361             splash = wx.GetApp().GetTopWindow()
 362             splash.OnClose(event)
 363 
 364         event.Skip()
 365 
 366 
 367     def OnDestroy(self, event):
 368         """
 369         ...
 370         """
 371 
 372         event.Skip()
 373 
 374 #---------------------------------------------------------------------------
 375 
 376 class MySplash(wx.Frame):
 377     """
 378     ...
 379     """
 380     def __init__(self):
 381 
 382         #--------------
 383 
 384         screen = wx.ScreenDC()
 385 
 386         # Screen size.
 387         ws, hs = screen.GetSize()
 388 
 389         #--------------
 390 
 391         # Return bitmaps folder.
 392         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 393 
 394         # Load a background bitmap.
 395         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 396                                           "rocket.png"),
 397                              type=wx.BITMAP_TYPE_PNG)
 398 
 399         mask = wx.Mask(self.bmp, wx.RED)
 400         self.bmp.SetMask(mask)
 401 
 402         # Determine size of bitmap.
 403         wi, hi = self.bmp.GetWidth(), self.bmp.GetHeight()
 404         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 405 
 406         x = int((ws-wi)/2)
 407         y = int((hs-hi)/2)
 408 
 409         #--------------
 410 
 411         super(MySplash, self).__init__(parent=None,
 412                                        id=-1,
 413                                        title="SplashScreen",
 414                                        pos=(x, y),
 415                                        size=(wi, hi),
 416                                        style=wx.FRAME_SHAPED |
 417                                              wx.BORDER_NONE |
 418                                              wx.FRAME_NO_TASKBAR |
 419                                              wx.STAY_ON_TOP)
 420 
 421         #--------------
 422 
 423         self.SetBackgroundColour("#344050")
 424 
 425         #--------------
 426 
 427         # Attributes.
 428         self.SetTransparent(0)
 429         self.opacity_in = 0
 430         self.deltaN = -30
 431         self.hasShape = False
 432 
 433         #--------------
 434 
 435         if wx.Platform != "__WXMAC__":
 436             # wxMac clips the tooltip to the window shape, YUCK!!!
 437             self.SetToolTip("Right-click to close the window\n"
 438                             "Double-click the image to set/unset the window shape")
 439 
 440         if wx.Platform == "__WXGTK__":
 441             # wxGTK requires that the window be created before you can
 442             # set its shape, so delay the call to SetWindowShape until
 443             # this event.
 444             self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
 445         else:
 446             # On wxMSW and wxMac the window has already
 447             # been created, so go for it.
 448             self.SetWindowShape()
 449 
 450         #--------------
 451 
 452         # Starts the Timer. Once Expired, splash is Destroyed.
 453         self.timer = wx.Timer(self)
 454 
 455         # Simulate long startup time.
 456         self.timer.Start(4000)
 457 
 458         self.Bind(wx.EVT_TIMER, self.TimeOut, self.timer)
 459 
 460         #--------------
 461 
 462         # Show main frame after 3000 ms.
 463         self.fc = wx.CallLater(3000, self.ShowMainFrame)
 464 
 465         #--------------
 466 
 467         self.CenterOnScreen(wx.BOTH)
 468         self.Show(True)
 469 
 470         #--------------
 471 
 472         # Simplified init method.
 473         self.OnTimerIn(self)
 474         self.CreateCtrls()
 475         self.DoLayout()
 476 
 477         #--------------
 478 
 479         print("\n... Display the splashScreen")
 480 
 481         #--------------
 482 
 483         self.SetClientSize((wi, hi))
 484         wx.BeginBusyCursor()
 485 
 486     #-----------------------------------------------------------------------
 487 
 488     def OnTimerIn(self, evt):
 489         """
 490         Thanks to Pascal Faut.
 491         """
 492 
 493         self.timer1 = wx.Timer(self, -1)
 494         self.timer1.Start(1)
 495         self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
 496 
 497         print("Fade-in was launched.")
 498 
 499 
 500     def AlphaCycle1(self, *args):
 501         """
 502         Thanks to Pascal Faut.
 503         """
 504 
 505         self.opacity_in += self.deltaN
 506         if self.opacity_in <= 0:
 507             self.deltaN = -self.deltaN
 508             self.opacity_in = 0
 509 
 510         if self.opacity_in >= 255:
 511             self.deltaN = -self.deltaN
 512             self.opacity_in = 255
 513 
 514             self.timer1.Stop()
 515 
 516         self.SetTransparent(self.opacity_in)
 517 
 518         print("Fade in = {}/255".format(self.opacity_in))
 519 
 520 
 521     def CreateCtrls(self):
 522         """
 523         ...
 524         """
 525 
 526         self.panel = MyPanel(self)
 527 
 528 
 529     def BindEvents(self):
 530         """
 531         Bind all the events related to my app.
 532         """
 533 
 534         # Bind some events to an events handler.
 535         self.Bind(wx.EVT_CHAR, self.OnCharEvents)
 536         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 537         self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnClose)
 538 
 539 
 540     def DoLayout(self):
 541         """
 542         ...
 543         """
 544 
 545         sizer = wx.BoxSizer(wx.VERTICAL)
 546         sizer.Add(self.panel, 1, wx.EXPAND)
 547         self.SetSizer(sizer)
 548 
 549 
 550     def SetWindowShape(self, *evt):
 551         """
 552         ...
 553         """
 554 
 555         # Use the bitmap's mask to determine the region.
 556         r = wx.Region(self.bmp)
 557         self.hasShape = self.SetShape(r)
 558 
 559 
 560     def OnCharEvents(self, event):
 561         """
 562         Handles the wx.EVT_CHAR for Splash.
 563         This reproduces the behavior of wx.SplashScreen.
 564         """
 565 
 566         self.OnClose(event)
 567 
 568 
 569     def TimeOut(self, event):
 570         """
 571         ...
 572         """
 573 
 574         self.Close(True)
 575 
 576 
 577     def OnCloseWindow(self, event):
 578         """
 579         ...
 580         """
 581 
 582         print("\n... Close timer")
 583 
 584         #--------------
 585 
 586         if hasattr(self, "timer"):
 587             self.Show(False)
 588             self.timer.Stop()
 589             del self.timer
 590 
 591 
 592     def OnClose(self, event):
 593         """
 594         Handles the wx.EVT_CLOSE event for SplashScreen.
 595         """
 596 
 597         # Make sure the default handler runs
 598         # too so this window gets destroyed.
 599         # Tell the event system to continue
 600         # looking for an event handler, so the
 601         # default handler will get called.
 602         event.Skip()
 603         self.Hide()
 604 
 605         #------------
 606 
 607         # If the timer is still running then go
 608         # ahead and show the main frame now.
 609         if self.fc.IsRunning():
 610             # Stop the wx.CallLater timer.
 611             # Stop the splash screen timer
 612             # and close it.
 613             self.fc.Stop()
 614             self.ShowMainFrame()
 615 
 616 
 617     def ShowMainFrame(self):
 618         """
 619         ...
 620         """
 621 
 622         print("\n... Close the splash screen")
 623         print("\n... Create and display the main frame")
 624 
 625         #------------
 626 
 627         wx.CallAfter(wx.EndBusyCursor)
 628 
 629         #------------
 630 
 631         if self.fc.IsRunning():
 632             # Stop the splash screen
 633             # timer and close it.
 634             self.Raise()
 635 
 636         #------------
 637 
 638         # Create an instance of the MyFrame class.
 639         frame = MyFrame()
 640 
 641 #---------------------------------------------------------------------------
 642 
 643 class MyApp(wx.App):
 644     """
 645     ...
 646     """
 647     def OnInit(self):
 648 
 649         #------------
 650 
 651         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 652 
 653         #------------
 654 
 655         self.SetAppName("Main frame")
 656 
 657         #------------
 658 
 659         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 660 
 661         #------------
 662 
 663         splash = MySplash()
 664         splash.BindEvents()
 665         splash.CenterOnScreen(wx.BOTH)
 666 
 667         return True
 668 
 669     #-----------------------------------------------------------------------
 670 
 671     def GetInstallDir(self):
 672         """
 673         Returns the installation directory for my application.
 674         """
 675 
 676         return self.installDir
 677 
 678 
 679     def GetIconsDir(self):
 680         """
 681         Returns the icons directory for my application.
 682         """
 683 
 684         icons_dir = os.path.join(self.installDir, "icons")
 685         return icons_dir
 686 
 687 
 688     def GetBitmapsDir(self):
 689         """
 690         Returns the bitmaps directory for my application.
 691         """
 692 
 693         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 694         return bitmaps_dir
 695 
 696 #---------------------------------------------------------------------------
 697 
 698 def main():
 699     app = MyApp(False)
 700     app.MainLoop()
 701 
 702 #---------------------------------------------------------------------------
 703 
 704 if __name__ == "__main__" :
 705     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     def OnCloseMe(self, event):
 110         """
 111         ...
 112         """
 113 
 114         self.Close(True)
 115 
 116 
 117     def OnCloseWindow(self, event):
 118         """
 119         ...
 120         """
 121 
 122         self.Destroy()
 123 
 124 #---------------------------------------------------------------------------
 125 
 126 class MySplash(SplashScreen):
 127     """
 128     ...
 129     """
 130     def __init__(self):
 131 
 132         self.SaveScreen()
 133 
 134         #------------
 135 
 136         # Return bitmaps folder.
 137         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 138 
 139         #------------
 140 
 141         # Load a background bitmap.
 142         bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 143                                      "splashscreen.bmp"),
 144                         type=wx.BITMAP_TYPE_BMP)
 145 
 146         # Determine size of bitmap.
 147         imgSize = (bmp.GetWidth(), bmp.GetHeight())
 148         print("\n... Bitmap size : %sx%s px" % (imgSize))
 149 
 150         #------------
 151 
 152         super(MySplash, self).__init__(bitmap=bmp,
 153                                        splashStyle=wx.adv.SPLASH_TIMEOUT,
 154                                        milliseconds=4000,
 155                                        parent=None,
 156                                        id=-1,
 157                                        pos=wx.DefaultPosition,
 158                                        size=imgSize,
 159                                        style=wx.STAY_ON_TOP |
 160                                              wx.BORDER_NONE)
 161 
 162         #------------
 163 
 164         # Simplified init method.
 165         self.BindEvents()
 166 
 167         #------------
 168 
 169         print("\n... Display the splashScreen")
 170 
 171         #--------------
 172 
 173         wx.BeginBusyCursor()
 174 
 175     #-----------------------------------------------------------------------
 176 
 177     def BindEvents(self):
 178         """
 179         ...
 180         """
 181 
 182         # Bind events to an events handler.
 183         self.Bind(wx.EVT_CLOSE, self.OnClose)
 184 
 185 
 186     def SaveScreen(self):
 187         """
 188         ...
 189         """
 190 
 191         # Return bitmaps folder.
 192         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 193 
 194         # Load a bitmap.
 195         bmp2 = wx.Bitmap(os.path.join(self.bitmaps_dir,
 196                                       "shadow.png"),
 197                          type=wx.BITMAP_TYPE_PNG)
 198 
 199         # Bitmap size.
 200         w2, h2 = bmp2.GetSize()
 201 
 202         #------------
 203 
 204         # Create a screenshot.
 205         screen = wx.ScreenDC()
 206 
 207         # Screen size.
 208         w, h = screen.GetSize()
 209         print("\n... Screen size : %sx%s px" % (w, h))
 210 
 211         bmp = wx.Bitmap(w, h)
 212 
 213         mem = wx.MemoryDC(screen)
 214         mem.SelectObject(bmp)
 215         mem.Blit(0, 0, w, h, screen, 0, 0)
 216         # Draw a bitmap with an alpha
 217         # channel on the screenshot.
 218         # image, x, y, transparency.
 219         mem.DrawBitmap(bmp2, int((w-w2)/2), int((h-h2)/2), False)
 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.bmp"),
 227                      type=wx.BITMAP_TYPE_BMP)
 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     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 
 127 #---------------------------------------------------------------------------
 128 
 129 class MyCaptionBox(wx.Panel):
 130     """
 131     ...
 132     """
 133     def __init__(self, parent, caption):
 134         super(MyCaptionBox, self).__init__(parent,
 135                                            style=wx.NO_BORDER |
 136                                                  wx.TAB_TRAVERSAL)
 137 
 138         #------------
 139 
 140         self.SetBackgroundColour("#344050")
 141 
 142         #------------
 143 
 144         # Attributes.
 145         self._caption = caption
 146         self._csizer = wx.BoxSizer(wx.VERTICAL)
 147 
 148         #------------
 149 
 150         # Simplified init method.
 151         self.BindEvents()
 152         self.DoLayout()
 153 
 154     #-----------------------------------------------------------------------
 155 
 156     def BindEvents(self):
 157         """
 158         ...
 159         """
 160 
 161         # Bind events to an events handler.
 162         self.Bind(wx.EVT_PAINT, self.OnPaint)
 163 
 164 
 165     def DoLayout(self):
 166         """
 167         ...
 168         """
 169 
 170         msizer = wx.BoxSizer(wx.HORIZONTAL)
 171         self._csizer.AddSpacer(15) # Extra space for caption.
 172         msizer.Add(self._csizer, 0, wx.EXPAND)
 173         self.SetSizer(msizer)
 174 
 175 
 176     def DoGetBestSize(self):
 177         """
 178         ...
 179         """
 180 
 181         size = super(MyCaptionBox, self).DoGetBestSize()
 182         # Compensate for wide caption labels.
 183         tw = self.GetTextExtent(self._caption)[0]
 184         size.SetWidth(max(size.width, tw))
 185         return size
 186 
 187 
 188     def AddItem(self, item):
 189         """
 190         Add a window or sizer item to the caption box.
 191         """
 192 
 193         self._csizer.Add(item, 0, wx.ALL, 10)
 194 
 195 
 196     def OnPaint(self, event):
 197         """
 198         Draws the Caption and border around the controls.
 199         """
 200 
 201         dc = wx.BufferedPaintDC(self)
 202         dc.SetBackground(wx.Brush(wx.Colour(52, 64, 80, 255)))
 203         dc.Clear()
 204         gcdc = wx.GCDC(dc)
 205 
 206         # Get the working rectangle we can draw in.
 207         rect = self.GetClientRect()
 208 
 209         # Get the system color to draw the caption.
 210         ss = wx.SystemSettings
 211         color = ss.GetColour(wx.SYS_COLOUR_3DDKSHADOW)
 212         gcdc.SetTextForeground(color)
 213         
 214         # Draw the border.
 215         rect.Inflate(-0, -0)
 216         gcdc.SetPen(wx.Pen(color))
 217         gcdc.SetBrush(wx.Brush(wx.Colour(52, 64, 80, 255)))
 218 
 219         # Font size and style.
 220         font = self.GetFont().GetPointSize()
 221         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
 222         font.SetWeight(wx.BOLD)
 223 
 224         # Add the Caption.
 225         rect = wx.Rect(rect.x, rect.y, rect.width, rect.height-34)
 226         rect.Inflate(-5, 0)
 227         gcdc.SetFont(font)
 228         gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
 229 
 230 #---------------------------------------------------------------------------
 231 
 232 class MyPanel(wx.Panel):
 233     """
 234     ...
 235     """
 236     def __init__(self, parent):
 237         wx.Panel.__init__(self, parent)
 238 
 239         #------------
 240 
 241         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
 242 
 243         #------------
 244 
 245         # Return bitmaps folder.
 246         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 247 
 248         # Load a background bitmap.
 249         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 250                                           "throbber.png"),
 251                              type=wx.BITMAP_TYPE_PNG)
 252 
 253         #------------
 254 
 255         # Attributes.
 256         self.box1 = MyCaptionBox(self, "Loading...")
 257 
 258         #------------
 259 
 260         # Throbber image list.
 261         self.images = [throb_images.catalog[img].GetBitmap()
 262                        for img in throb_images.index
 263                        if img not in ["eclouds", "logo"]]
 264 
 265 
 266         # Put a throbber in the box.
 267         self.throb = throb.Throbber(self.box1,
 268                                     -1,
 269                                     self.images,
 270                                     frameDelay=0.1,
 271                                     rest=4)
 272         self.box1.AddItem(self.throb)
 273 
 274         self.throb.Start()
 275 
 276         #------------
 277 
 278         # Simplified init method.
 279         self.BindEvents()
 280         self.DoLayout()
 281 
 282         #------------
 283 
 284         self.SetClientSize((262, 262))
 285 
 286     #-----------------------------------------------------------------------
 287 
 288     def BindEvents(self):
 289         """
 290         ...
 291         """
 292 
 293         # Bind some events to an events handler.
 294         self.Bind(wx.EVT_PAINT, self.OnPaint)
 295         self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
 296         self.box1.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 297         self.throb.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 298 
 299 
 300     def DoLayout(self):
 301         """
 302         ...
 303         """
 304 
 305         hsizer = wx.BoxSizer(wx.HORIZONTAL)
 306         vsizer = wx.BoxSizer(wx.VERTICAL)
 307 
 308         #------------
 309 
 310         # Add the box to the panel.
 311         hsizer.AddStretchSpacer()
 312         hsizer.Add(self.box1, 0, wx.ALIGN_CENTER)
 313         vsizer.AddStretchSpacer(40)
 314         vsizer.Add(hsizer, 0, wx.ALIGN_CENTER)
 315         vsizer.AddStretchSpacer()
 316 
 317         self.SetSizer(vsizer)
 318 
 319 
 320     def OnPaint(self, event):
 321         """
 322         ...
 323         """
 324 
 325         dc = wx.BufferedPaintDC(self)
 326         dc.Clear()
 327         gcdc = wx.GCDC(dc)
 328         gcdc.Clear()
 329 
 330         # Draw a bitmap with an alpha channel
 331         # on top of the last group.
 332         gcdc.DrawBitmap(self.bmp, 0, 0, False)
 333 
 334 
 335     def OnMouseEvents(self, event):
 336         """
 337         Handles the wx.EVT_MOUSE_EVENTS for AdvancedSplash.
 338         This reproduces the behavior of wx.SplashScreen.
 339         """
 340 
 341         if event.LeftDown() or event.RightDown():
 342             splash = wx.GetApp().GetTopWindow()
 343             splash.OnClose(event)
 344 
 345         event.Skip()
 346 
 347 
 348     def OnDestroy(self, event):
 349         """
 350         ...
 351         """
 352 
 353         self.throb.Stop()
 354         event.Skip()
 355 
 356 #---------------------------------------------------------------------------
 357 
 358 class MySplash(wx.Frame):
 359     """
 360     ...
 361     """
 362     def __init__(self):
 363 
 364         #--------------
 365 
 366         screen = wx.ScreenDC()
 367 
 368         # Screen size.
 369         ws, hs = screen.GetSize()
 370 
 371         #--------------
 372 
 373         # Return bitmaps folder.
 374         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 375 
 376         # Load a background bitmap.
 377         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 378                                           "throbber.png"),
 379                              type=wx.BITMAP_TYPE_PNG)
 380 
 381         mask = wx.Mask(self.bmp, wx.RED)
 382         self.bmp.SetMask(mask)
 383 
 384         # Determine size of bitmap.
 385         wi, hi = self.bmp.GetWidth(), self.bmp.GetHeight()
 386         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 387 
 388         x = int((ws-wi)/2)
 389         y = int((hs-hi)/2)
 390 
 391         #--------------
 392 
 393         super(MySplash, self).__init__(parent=None,
 394                                        id=-1,
 395                                        title="SplashScreen",
 396                                        pos=(x, y),
 397                                        size=(wi, hi),
 398                                        style=wx.FRAME_SHAPED |
 399                                              wx.BORDER_NONE |
 400                                              wx.FRAME_NO_TASKBAR |
 401                                              wx.STAY_ON_TOP)
 402 
 403         #--------------
 404 
 405         self.SetBackgroundColour("#4a6991")
 406 
 407         #--------------
 408 
 409         # Attributes.
 410         self.SetTransparent(0)
 411         self.opacity_in = 0
 412         self.deltaN = -30
 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.OnTimerIn(self)
 456         self.CreateCtrls()
 457         self.DoLayout()
 458 
 459         #--------------
 460 
 461         print("\n... Display the splashScreen")
 462 
 463         #--------------
 464 
 465         self.SetClientSize((wi, hi))
 466         wx.BeginBusyCursor()
 467 
 468     #-----------------------------------------------------------------------
 469 
 470     def OnTimerIn(self, evt):
 471         """
 472         Thanks to Pascal Faut.
 473         """
 474 
 475         self.timer1 = wx.Timer(self, -1)
 476         self.timer1.Start(1)
 477         self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
 478 
 479         print("Fade-in was launched.")
 480 
 481 
 482     def AlphaCycle1(self, *args):
 483         """
 484         Thanks to Pascal Faut.
 485         """
 486 
 487         self.opacity_in += self.deltaN
 488         if self.opacity_in <= 0:
 489             self.deltaN = -self.deltaN
 490             self.opacity_in = 0
 491 
 492         if self.opacity_in >= 255:
 493             self.deltaN = -self.deltaN
 494             self.opacity_in = 255
 495 
 496             self.timer1.Stop()
 497 
 498         self.SetTransparent(self.opacity_in)
 499 
 500         print("Fade in = {}/255".format(self.opacity_in))
 501 
 502 
 503     def CreateCtrls(self):
 504         """
 505         ...
 506         """
 507 
 508         self.panel = MyPanel(self)
 509 
 510 
 511     def BindEvents(self):
 512         """
 513         Bind all the events related to my app.
 514         """
 515 
 516         # Bind some events to an events handler.
 517         self.Bind(wx.EVT_CHAR, self.OnCharEvents)
 518         self.Bind(wx.EVT_CLOSE, self.OnClose)
 519         self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnClose)
 520         self.Bind(wx.EVT_CLOSE, self.CloseWindow)
 521 
 522 
 523     def DoLayout(self):
 524         """
 525         ...
 526         """
 527 
 528         sizer = wx.BoxSizer(wx.VERTICAL)
 529         sizer.Add(self.panel, 1, wx.EXPAND, 20)
 530         self.SetSizer(sizer)
 531 
 532 
 533     def SetWindowShape(self, *evt):
 534         """
 535         ...
 536         """
 537 
 538         # Use the bitmap's mask to determine the region.
 539         r = wx.Region(self.bmp)
 540         self.hasShape = self.SetShape(r)
 541 
 542 
 543     def OnCharEvents(self, event):
 544         """
 545         Handles the wx.EVT_CHAR for Splash.
 546         This reproduces the behavior of wx.SplashScreen.
 547         """
 548 
 549         self.OnClose(event)
 550 
 551 
 552     def TimeOut(self, event):
 553         """
 554         ...
 555         """
 556 
 557         self.Close(True)
 558 
 559 
 560     def OnClose(self, event):
 561         """
 562         Handles the wx.EVT_CLOSE event for SplashScreen.
 563         """
 564 
 565         # Make sure the default handler runs
 566         # too so this window gets destroyed.
 567         # Tell the event system to continue
 568         # looking for an event handler, so the
 569         # default handler will get called.
 570         # event.Skip()  # ?????
 571         self.Hide()
 572 
 573         #------------
 574 
 575         # If the timer is still running then go
 576         # ahead and show the main frame now.
 577         if self.fc.IsRunning():
 578             # Stop the wx.CallLater timer.
 579             # Stop the splash screen timer
 580             # and close it.
 581             self.fc.Stop()
 582             self.ShowMainFrame()
 583 
 584 
 585     def ShowMainFrame(self):
 586         """
 587         ...
 588         """
 589 
 590         print("\n... Close the splash screen")
 591         print("\n... Create and display the main frame")
 592 
 593         #------------
 594 
 595         wx.CallAfter(wx.EndBusyCursor)
 596 
 597         #------------
 598 
 599         if self.fc.IsRunning():
 600             # Stop the splash screen
 601             # timer and close it.
 602             self.Raise()
 603 
 604         #------------
 605 
 606         # Create an instance of the MyFrame class.
 607         frame = MyFrame()
 608 
 609 
 610     def CloseWindow(self, event):
 611         """
 612         ...
 613         """
 614 
 615         self.timer.Stop()
 616         self.Destroy()
 617 
 618 #---------------------------------------------------------------------------
 619 
 620 class MyApp(wx.App):
 621     """
 622     ...
 623     """
 624     def OnInit(self):
 625 
 626         #------------
 627 
 628         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 629 
 630         #------------
 631 
 632         self.SetAppName("Main frame")
 633 
 634         #------------
 635 
 636         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 637 
 638         #------------
 639 
 640         splash = MySplash()
 641         splash.BindEvents()
 642         splash.CenterOnScreen(wx.BOTH)
 643 
 644         return True
 645 
 646     #-----------------------------------------------------------------------
 647 
 648     def GetInstallDir(self):
 649         """
 650         Returns the installation directory for my application.
 651         """
 652 
 653         return self.installDir
 654 
 655 
 656     def GetIconsDir(self):
 657         """
 658         Returns the icons directory for my application.
 659         """
 660 
 661         icons_dir = os.path.join(self.installDir, "icons")
 662         return icons_dir
 663 
 664 
 665     def GetBitmapsDir(self):
 666         """
 667         Returns the bitmaps directory for my application.
 668         """
 669 
 670         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 671         return bitmaps_dir
 672 
 673 #---------------------------------------------------------------------------
 674 
 675 def main():
 676     app = MyApp(False)
 677     app.MainLoop()
 678 
 679 #---------------------------------------------------------------------------
 680 
 681 if __name__ == "__main__" :
 682     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     def OnCloseMe(self, event):
 112         """
 113         ...
 114         """
 115 
 116         self.Close(True)
 117 
 118 
 119     def OnCloseWindow(self, event):
 120         """
 121         ...
 122         """
 123 
 124         self.Destroy()
 125 
 126 #---------------------------------------------------------------------------
 127 
 128 class MyCaptionBox(wx.Panel):
 129     """
 130     ...
 131     """
 132     def __init__(self, parent, caption):
 133         super(MyCaptionBox, self).__init__(parent,
 134                                            style=wx.NO_BORDER |
 135                                                  wx.TAB_TRAVERSAL)
 136 
 137         #------------
 138 
 139         self.SetBackgroundColour("white")
 140 
 141         #------------
 142 
 143         # Attributes.
 144         self._caption = caption
 145         self._csizer = wx.BoxSizer(wx.VERTICAL)
 146 
 147         #------------
 148 
 149         # Simplified init method.
 150         self.BindEvents()
 151         self.DoLayout()
 152 
 153     #-----------------------------------------------------------------------
 154 
 155     def BindEvents(self):
 156         """
 157         ...
 158         """
 159 
 160         # Bind events to an events handler.
 161         self.Bind(wx.EVT_PAINT, self.OnPaint)
 162 
 163 
 164     def DoLayout(self):
 165         """
 166         ...
 167         """
 168 
 169         msizer = wx.BoxSizer(wx.HORIZONTAL)
 170         self._csizer.AddSpacer(15) # Extra space for caption.
 171         msizer.Add(self._csizer, 0, wx.EXPAND)
 172         self.SetSizer(msizer)
 173 
 174 
 175     def DoGetBestSize(self):
 176         """
 177         ...
 178         """
 179 
 180         size = super(MyCaptionBox, self).DoGetBestSize()
 181         # Compensate for wide caption labels.
 182         tw = self.GetTextExtent(self._caption)[0]
 183         size.SetWidth(max(size.width, tw))
 184         return size
 185 
 186 
 187     def AddItem(self, item):
 188         """
 189         Add a window or sizer item to the caption box.
 190         """
 191 
 192         self._csizer.Add(item, 0, wx.ALL, 10)
 193 
 194 
 195     def OnPaint(self, event):
 196         """
 197         Draws the Caption and border around the controls.
 198         """
 199 
 200         dc = wx.BufferedPaintDC(self)
 201         dc.SetBackground(wx.Brush(wx.Colour(255, 255, 255, 255)))
 202         dc.Clear()
 203         gcdc = wx.GCDC(dc)
 204 
 205         # Get the working rectangle we can draw in.
 206         rect = self.GetClientRect()
 207 
 208         # Get the sytem color to draw the caption.
 209         ss = wx.SystemSettings
 210         color = ss.GetColour(wx.SYS_COLOUR_3DDKSHADOW)
 211         gcdc.SetTextForeground(wx.BLACK)
 212 
 213         # Draw the border.
 214         rect.Inflate(-0, -0)
 215         gcdc.SetPen(wx.Pen(color))
 216         gcdc.SetBrush(wx.Brush(wx.Colour(52, 64, 80, 255)))
 217 
 218         # Font size and style.
 219         font = self.GetFont().GetPointSize()
 220         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
 221         font.SetWeight(wx.BOLD)
 222 
 223         # Add the Caption.
 224         rect = wx.Rect(rect.x, rect.y, rect.width, 20)
 225         rect.Inflate(-5, 0)
 226         gcdc.SetFont(font)
 227         gcdc.DrawLabel(self._caption, rect, wx.ALIGN_CENTER)
 228 
 229 #---------------------------------------------------------------------------
 230 
 231 class MyPanel(wx.Panel):
 232     """
 233     ...
 234     """
 235     def __init__(self, parent):
 236         wx.Panel.__init__(self, parent)
 237 
 238         #------------
 239 
 240         self.SetBackgroundColour("white")
 241 
 242         #------------
 243 
 244         # Return bitmaps folder.
 245         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 246 
 247         # Load a background bitmap.
 248         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 249                                           "gloss.png"),
 250                              type=wx.BITMAP_TYPE_PNG)
 251 
 252         #------------
 253 
 254         # Attributes.
 255         self.box1 = MyCaptionBox(self, "Loading...")
 256 
 257         #------------
 258 
 259         # Pick the filename of an animated GIF file you have ...
 260         # Give it the full path and file name.
 261         animatedGif = Animation(os.path.join(self.bitmaps_dir,
 262                                              "loader.gif"))
 263         self.ag = AnimationCtrl(self.box1,
 264                                 -1,
 265                                 animatedGif)
 266 
 267         # Clears the background.
 268         self.ag.SetBackgroundColour(self.GetBackgroundColour())
 269 
 270         # Continuously loop through the frames of
 271         # the gif file (default).
 272         self.ag.Play()
 273 
 274         self.box1.AddItem(self.ag)
 275 
 276         #------------
 277 
 278         # Simplified init method.
 279         self.BindEvents()
 280         self.DoLayout()
 281 
 282         #------------
 283 
 284         self.SetClientSize((262, 282))
 285 
 286     #-----------------------------------------------------------------------
 287 
 288     def BindEvents(self):
 289         """
 290         ...
 291         """
 292 
 293         # Bind some events to an events handler.
 294         self.Bind(wx.EVT_PAINT, self.OnPaint)
 295         self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
 296         self.box1.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 297         self.ag.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
 298 
 299 
 300     def DoLayout(self):
 301         """
 302         ...
 303         """
 304 
 305         hsizer = wx.BoxSizer(wx.HORIZONTAL)
 306         vsizer = wx.BoxSizer(wx.VERTICAL)
 307 
 308         #------------
 309 
 310         # Add the box to the panel.
 311         hsizer.AddStretchSpacer()
 312         hsizer.Add(self.box1, 0, wx.ALIGN_CENTER)
 313         vsizer.AddStretchSpacer(40)
 314         vsizer.Add(hsizer, 0, wx.ALIGN_CENTER)
 315         vsizer.AddStretchSpacer()
 316 
 317         self.SetSizer(vsizer)
 318 
 319 
 320     def OnPaint(self, event):
 321         """
 322         ...
 323         """
 324 
 325         dc = wx.BufferedPaintDC(self)
 326         dc.Clear()
 327         gcdc = wx.GCDC(dc)
 328         gcdc.Clear()
 329 
 330         # Draw a bitmap with an alpha channel
 331         # on top of the last group.
 332         gcdc.DrawBitmap(self.bmp, 0, 0, False)
 333 
 334 
 335     def OnMouseEvents(self, event):
 336         """
 337         Handles the wx.EVT_MOUSE_EVENTS for AdvancedSplash.
 338         This reproduces the behavior of wx.SplashScreen.
 339         """
 340 
 341         if event.LeftDown() or event.RightDown():
 342             splash = wx.GetApp().GetTopWindow()
 343             splash.OnClose(event)
 344 
 345         event.Skip()
 346 
 347 
 348     def OnDestroy(self, event):
 349         """
 350         ...
 351         """
 352 
 353         event.Skip()
 354 
 355 #---------------------------------------------------------------------------
 356 
 357 class MySplash(wx.Frame):
 358     """
 359     ...
 360     """
 361     def __init__(self):
 362 
 363         #--------------
 364 
 365         screen = wx.ScreenDC()
 366 
 367         # Screen size.
 368         ws, hs = screen.GetSize()
 369 
 370         #------------
 371 
 372         # Return bitmaps folder.
 373         self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
 374 
 375         # Load a background bitmap.
 376         self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
 377                                           "gloss.png"),
 378                              type=wx.BITMAP_TYPE_PNG)
 379 
 380         mask = wx.Mask(self.bmp, wx.RED)
 381         self.bmp.SetMask(mask)
 382 
 383         # Determine size of bitmap.
 384         wi, hi = self.bmp.GetWidth(), self.bmp.GetHeight()
 385         print("\n... Bitmap size : %sx%s px" % (wi, hi))
 386 
 387         x = int((ws-wi)/2)
 388         y = int((hs-hi)/2)
 389 
 390         #--------------
 391 
 392         super(MySplash, self).__init__(parent=None,
 393                                        id=-1,
 394                                        title="SplashScreen",
 395                                        pos=(x, y),
 396                                        size=(wi, hi),
 397                                        style=wx.FRAME_SHAPED |
 398                                              wx.BORDER_NONE |
 399                                              wx.FRAME_NO_TASKBAR |
 400                                              wx.STAY_ON_TOP)
 401 
 402         #--------------
 403 
 404         self.SetBackgroundColour("white")
 405 
 406         #--------------
 407 
 408         # Attributes.
 409         self.SetTransparent(0)
 410         self.opacity_in = 0
 411         self.deltaN = -30
 412         self.hasShape = False
 413 
 414         #--------------
 415 
 416         if wx.Platform != "__WXMAC__":
 417             # wxMac clips the tooltip to the window shape, YUCK!!!
 418             self.SetToolTip("Right-click to close the window\n"
 419                             "Double-click the image to set/unset the window shape")
 420 
 421         if wx.Platform == "__WXGTK__":
 422             # wxGTK requires that the window be created before you can
 423             # set its shape, so delay the call to SetWindowShape until
 424             # this event.
 425             self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
 426         else:
 427             # On wxMSW and wxMac the window has already
 428             # been created, so go for it.
 429             self.SetWindowShape()
 430 
 431         #--------------
 432 
 433         # Starts the Timer. Once Expired, splash is Destroyed.
 434         self.timer = wx.Timer(self)
 435 
 436         # Simulate long startup time.
 437         self.timer.Start(4000)
 438 
 439         self.Bind(wx.EVT_TIMER, self.TimeOut, self.timer)
 440 
 441         #--------------
 442 
 443         # Show main frame after 3000 ms.
 444         self.fc = wx.CallLater(3000, self.ShowMainFrame)
 445 
 446         #--------------
 447 
 448         self.CenterOnScreen(wx.BOTH)
 449         self.Show(True)
 450 
 451         #--------------
 452 
 453         # Simplified init method.
 454         self.OnTimerIn(self)
 455         self.CreateCtrls()
 456         self.DoLayout()
 457 
 458         #--------------
 459 
 460         print("\n... Display the splashScreen")
 461 
 462         #--------------
 463 
 464         self.SetClientSize((wi, hi))
 465         wx.BeginBusyCursor()
 466 
 467     #-----------------------------------------------------------------------
 468 
 469     def OnTimerIn(self, evt):
 470         """
 471         Thanks to Pascal Faut.
 472         """
 473 
 474         self.timer1 = wx.Timer(self, -1)
 475         self.timer1.Start(1)
 476         self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
 477 
 478         print("Fade-in was launched.")
 479 
 480 
 481     def AlphaCycle1(self, *args):
 482         """
 483         Thanks to Pascal Faut.
 484         """
 485 
 486         self.opacity_in += self.deltaN
 487         if self.opacity_in <= 0:
 488             self.deltaN = -self.deltaN
 489             self.opacity_in = 0
 490 
 491         if self.opacity_in >= 255:
 492             self.deltaN = -self.deltaN
 493             self.opacity_in = 255
 494 
 495             self.timer1.Stop()
 496 
 497         self.SetTransparent(self.opacity_in)
 498 
 499         print("Fade in = {}/255".format(self.opacity_in))
 500 
 501 
 502     def CreateCtrls(self):
 503         """
 504         ...
 505         """
 506 
 507 
 508         self.panel = MyPanel(self)
 509 
 510 
 511     def BindEvents(self):
 512         """
 513         Bind all the events related to my app.
 514         """
 515 
 516         # Bind some events to an events handler.
 517         self.Bind(wx.EVT_CHAR, self.OnCharEvents)
 518         self.Bind(wx.EVT_CLOSE, self.OnClose)
 519         self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnClose)
 520         self.Bind(wx.EVT_CLOSE, self.CloseWindow)
 521 
 522 
 523     def DoLayout(self):
 524         """
 525         ...
 526         """
 527 
 528         sizer = wx.BoxSizer(wx.VERTICAL)
 529         sizer.Add(self.panel, 1, wx.EXPAND, 20)
 530         self.SetSizer(sizer)
 531 
 532 
 533     def SetWindowShape(self, *evt):
 534         """
 535         ...
 536         """
 537 
 538         # Use the bitmap's mask to determine the region.
 539         r = wx.Region(self.bmp)
 540         self.hasShape = self.SetShape(r)
 541 
 542 
 543     def OnCharEvents(self, event):
 544         """
 545         Handles the wx.EVT_CHAR for Splash.
 546         This reproduces the behavior of wx.SplashScreen.
 547         """
 548 
 549         self.OnClose(event)
 550 
 551 
 552     def TimeOut(self, event):
 553         """
 554         ...
 555         """
 556 
 557         self.Close(True)
 558 
 559 
 560     def OnClose(self, event):
 561         """
 562         Handles the wx.EVT_CLOSE event for SplashScreen.
 563         """
 564 
 565         # Make sure the default handler runs
 566         # too so this window gets destroyed.
 567         # Tell the event system to continue
 568         # looking for an event handler, so the
 569         # default handler will get called.
 570         # event.Skip()  # ?????
 571         self.Hide()
 572 
 573         #------------
 574 
 575         # If the timer is still running then go
 576         # ahead and show the main frame now.
 577         if self.fc.IsRunning():
 578             # Stop the wx.CallLater timer.
 579             # Stop the splash screen timer
 580             # and close it.
 581             self.fc.Stop()
 582             self.ShowMainFrame()
 583 
 584 
 585     def ShowMainFrame(self):
 586         """
 587         ...
 588         """
 589 
 590         print("\n... Close the splash screen")
 591         print("\n... Create and display the main frame")
 592 
 593         #------------
 594 
 595         wx.CallAfter(wx.EndBusyCursor)
 596 
 597         #------------
 598 
 599         if self.fc.IsRunning():
 600             # Stop the splash screen
 601             # timer and close it.
 602             self.Raise()
 603 
 604         #------------
 605 
 606         # Create an instance of the MyFrame class.
 607         frame = MyFrame()
 608 
 609 
 610     def CloseWindow(self, event):
 611         """
 612         ...
 613         """
 614 
 615         self.timer.Stop()
 616         self.Destroy()
 617 
 618 #---------------------------------------------------------------------------
 619 
 620 class MyApp(wx.App):
 621     """
 622     ...
 623     """
 624     def OnInit(self):
 625 
 626         #------------
 627 
 628         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 629 
 630         #------------
 631 
 632         self.SetAppName("Main frame")
 633 
 634         #------------
 635 
 636         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 637 
 638         #------------
 639 
 640         splash = MySplash()
 641         splash.BindEvents()
 642         splash.CenterOnScreen(wx.BOTH)
 643 
 644         return True
 645 
 646     #-----------------------------------------------------------------------
 647 
 648     def GetInstallDir(self):
 649         """
 650         Returns the installation directory for my application.
 651         """
 652 
 653         return self.installDir
 654 
 655 
 656     def GetIconsDir(self):
 657         """
 658         Returns the icons directory for my application.
 659         """
 660 
 661         icons_dir = os.path.join(self.installDir, "icons")
 662         return icons_dir
 663 
 664 
 665     def GetBitmapsDir(self):
 666         """
 667         Returns the bitmaps directory for my application.
 668         """
 669 
 670         bitmaps_dir = os.path.join(self.installDir, "bitmaps")
 671         return bitmaps_dir
 672 
 673 #---------------------------------------------------------------------------
 674 
 675 def main():
 676     app = MyApp(False)
 677     app.MainLoop()
 678 
 679 #---------------------------------------------------------------------------
 680 
 681 if __name__ == "__main__" :
 682     main()


With masked area

img_sample_five.png