Introduction
Most GUI programs display a splash screen before launching real applications. This snippet demonstrates the basics of how to use a wx.SplashScreen in an application.
What Objects are Involved
wx.SplashScreen
The purpose of this wxPyWiki.
wx.Image
- Used to load a splash image of some type (e.g., JPEG, etc.).
wx.Frame
- Used to implement various widgets.
wx.App
Need I say more?
Process Overview
Once launched, this recipe shows a JPEG image (the wxPyWiki logo) at the center of a screen for one second:
Then this window titled "Hello from wxPython" appears:
Special Concerns
- I only tested on Windows XP SP2 with Python2.4 and wxPython 2.5.3.1.
A JPEG image named "wxPyWiki.jpg" must exist in the working directory.
Code Sample
##
## DO NOT EDIT
##
#----------------------------------------------------------------------#
# This is a minimal wxPython program to show a SplashScreen widget.
#
# Tian Xie
# 3/8/2005
#----------------------------------------------------------------------#
import wx
class MyGUI(wx.Frame):
"""
Hello World!
"""
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
# Put more GUI code here for a fancier application.
#----------------------------------------------------------------------#
class MySplashScreen(wx.SplashScreen):
"""
Create a splash screen widget.
"""
def __init__(self, parent=None):
# This is a recipe to a the screen.
# Modify the following variables as necessary.
aBitmap = wx.Image(name = "wxPyWiki.jpg").ConvertToBitmap()
splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT
splashDuration = 1000 # milliseconds
# Call the constructor with the above arguments in exactly the
# following order.
wx.SplashScreen.__init__(self, aBitmap, splashStyle,
splashDuration, parent)
self.Bind(wx.EVT_CLOSE, self.OnExit)
wx.Yield()
#----------------------------------------------------------------------#
def OnExit(self, evt):
self.Hide()
# MyFrame is the main frame.
MyFrame = MyGUI(None, -1, "Hello from wxPython")
app.SetTopWindow(MyFrame)
MyFrame.Show(True)
# The program will freeze without this line.
evt.Skip() # Make sure the default handler runs too...
#----------------------------------------------------------------------#
class MyApp(wx.App):
def OnInit(self):
MySplash = MySplashScreen()
MySplash.Show()
return True
#----------------------------------------------------------------------#
app = MyApp(redirect=True, filename = "demo.log")
app.MainLoop()
Comments
- Please test this program on other platforms or versions. If it doesn't work, find a fix and correct it here. Thanks.
Although this example will work fine, it overlooks the main reason programmers add a splash screen; to give the user feedback while a program is loading.
