Python Help - Splash Screen
Brian Kelley
bkelley at wi.mit.edu
Tue Mar 5 10:44:00 EST 2002
I stole this a while ago from the following post from
quickdry at users.sourceforge.net deja reference
http://groups.google.com/groups?q=tkinter+using+pil&hl=en&selm=3bf9d934%240%2410227%24afc38c87%40news.optusnet.com.au&rnum=1
I modified it to add PIL support for loading images. It shows a splash
screen with an image (jpg, gif and others that PIL supports) for a
specified number of milliseconds, then it closes and the main window
pops up. You will need to have PIL installed
http://www.pythonware.com/products/pil/
You'll have to supply your own "python.jpg" file to test :)
"""SplashScreen
A splash screen that displays an image for a predefined
number of milliseconds.
usage
tk = Tk()
s = SplashScreen(tk, timeout=2000, image="python.jpg")
mainloop()
The main window will be shown after the timeout.
requires: PIL imaging library
"""
from Tkinter import *
try:
from PIL import Image, ImageTk
except ImportError:
import Image, ImageTk
class SplashScreen(Toplevel):
def __init__(self, master, image=None, timeout=1000):
"""(master, image, timeout=1000) -> create a splash screen
from specified image file. Keep splashscreen up for timeout
milliseconds"""
Toplevel.__init__(self, master, relief=RAISED, borderwidth=5)
self.main = master
if self.main.master != None: # Why ?
self.main.master.withdraw()
self.main.withdraw()
self.overrideredirect(1)
im = Image.open(image)
self.image = ImageTk.PhotoImage(im)
self.after_idle(self.centerOnScreen)
self.update()
self.after(timeout, self.destroy)
def centerOnScreen(self):
self.update_idletasks()
width, height = self.width, self.height = \
self.image.width(), self.image.height()
xmax = self.winfo_screenwidth()
ymax = self.winfo_screenheight()
x0 = self.x0 = (xmax - self.winfo_reqwidth()) / 2 - width/2
y0 = self.y0 = (ymax - self.winfo_reqheight()) / 2 - height/2
self.geometry("+%d+%d" % (x0, y0))
self.createWidgets()
def createWidgets(self):
# Need to fill in here
self.canvas = Canvas(self, height=self.width, width=self.height)
self.canvas.create_image(0,0, anchor=NW, image=self.image)
self.canvas.pack()
def destroy(self):
self.main.update()
self.main.deiconify()
self.withdraw()
if __name__ == "__main__":
import os
tk = Tk()
l = Label(tk, text="main window")
l.pack()
assert os.path.exists("python.jpg")
s = SplashScreen(tk, timeout=2000, image="python.jpg")
mainloop()
More information about the Python-list
mailing list