It's easier to program a splash screen than you probably realize. That's our conclusion after recent chats with a couple of successful businesses.
There are plenty of facilities for tinkering with Linux splash screens, of course; at no charge you can download instructions that will help you customize your GNOME or Ubuntu or other splash screen, that is, the screen-filling image that first comes up when you launch your desktop. What we didn't realize, though, was how hard some developers work to put nice splash screens in individual applications. We've come across cases recently where vendors require multimegabyte libraries just to get the splash screen they want.
Tk at Your Fingertips
There's an easier way. While Java has its own answers, of course, even the lightweight languages "Regular Expressions" covers each month only need a few lines to achieve a nice-looking, capable, splash screen. One key is that languages like Python, Perl, and Tcl build in standard graphical user interface (GUI) capabilities with the Tk toolkit. That means that, even if your development is on odd-ball hardware--a low-power dedicated kiosk, for example--if you have one of these popular languages installed, you have everything you need for a splash screen.
Here's an example in Python:
#!/usr/bin/python
import Tkinter
master = Tkinter.Tk()
# Reproduced under license from United States
# Department of Agriculture.
# Download a copy from from <URL: http://phaseit.net/tmp/gif/s1.gif>.
image_file = "s1.gif"
image = Tkinter.PhotoImage(file = image_file)
# Fill the screen.
screen_width = master.winfo_screenwidth()
screen_height = master.winfo_screenheight()
s = Tkinter.Canvas(master, width = screen_width,
height = screen_height)
# Remove window-manager decorations: don't allow
# the user to destroy or iconify the splash screen.
master.overrideredirect(1)
# Center the image.
s.create_image(screen_width / 2, screen_height / 2,
image = image)
s.create_text( screen_width / 2, screen_height / 2,
text = "This is an example of a message.",
fill = "white", font = "normal 45")
s.pack()
# After five seconds--5000 milliseconds--remove the
# splash screen.
master.after(5000, lambda: master.destroy())
master.mainloop()
Run a script with these few lines, and your screen should fill up with an image like the one at right. It "takes over" your display, in the sense that there's nothing in general you can click or move to "see behind it". After five seconds, though, the splash screen removes itself, presumably to make way for the rest of your application, which will have had time to initialize itself.
Troubleshooting
That's an impressive achievement, at least in comparison to the charges we occasionally see developers bill for these sorts of results, and the bulky libraries they use. To ensure you can enjoy this script for yourself, and have a full opportunity to experiment with it, we'll list here a few of the hurdles you might encounter:
- "
... Can't open display: ... : DISPLAY is not set": under Linux, this script depends on an X server where it can display its results. If you operate in a character-oriented environment, you'll probably need a different approach to a splash screen. You might merely have a mis-configured shell, though, in which case a command likeexport DISPLAY=localhost:0.0could help. - Several years ago, it was common to stumble across problems with
import Tkinter. With most modern Linux distributions, though, Tkinter (as well as the corresponding modules for Perl, Tcl, and other languages) are already properly installed. In the worst case, it should take no more thanapt-get install python-tkor the equivalent to complete your configuration. - Version should be no problem. The code in this column works for any Python from release 2.4 to 3.1.
- Depending on the details of your environment, you might need to store the script in, for instance,
s1.py, then invoke it aspython s1.py.
A Web search for "splash screen Python" returns plenty of "hits"; does the world really need one more treatment of this subject?
Yes. All the other common explanations have difficulties: the code is wrapped up in an object-oriented style that makes it hard to see the basics of what's going on, or it depends on libraries that are touchy to install, or it simply doesn't work as presented. The version above includes the essentials necessary for a splash screen, but is simple enough to understand at a glance.
More Polish
To put a splash screen into production will probably take a bit more polish than the model code above; you might want to:
- configure different images and messages for different circumstances,
- put up a progress-bar that re-assures the user that the main application will be ready soon,
- bind special actions to a few keys,
- make the splash screen appear occasionally during operation of the application, not just on initial launch,
- experiment with special optical effects such as transparency, or
- so on.
Individually, each of these takes only a couple of lines more; a general-purpose splash screen with all these capabilities, though, takes several-hundred lines, once proper error-handling and documentation are included. That's the lesson of, for example Emmanuel Frécon's open-source Splash library. As much as we admire such work, the more important point for this column is that the heart of a solid implementation is small and understandable.
There's one slight complication we should explain: the code above goes to the trouble of removing window decorations with master.overrideredirect(1). This is a slightly controversial area, in that window-manager standardization is incomplete. By itself, for example, overrideredirect can't guarantee the user is blocked from seeing anything else on the screen. Different window managers vary in the way they handle toplevel windows. For the most common window managers, however, and certainly for the base installations of standard distributions including Ubuntu, SuSE, and Centos, the script which appears above gives good results.
Summary
Command-line work remains essential in daily Linux work. It's easy to forget, though, that basic GUI functionality only takes a few extra lines, because built-in libraries such as Tk are so mature. A customized splash-screen, for instance, is well within the reach of even a beginner in one of the the common scripting languages.
Kathryn and Cameron run their own consultancy, Phaseit, Inc., specializing in high-reliability and high-performance applications managed by high-level languages. Among other community responsibilities, Cameron moderates the Tkinter mailing list. Cameron and Kathryn write about scripting languages and related topics in their "Regular Expressions" columns.
