Customized splash screen (Phoenix)

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


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

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


With gauge :

First example

img_sample_one_a.png

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


With masked area

img_sample_five.png

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