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


With masked area

img_sample_five.png