I used to this code for a splash screen but doesn't run. Please could someone assist. Or help with a link I can check Tkinter splash screens.

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="Phone Book")
     l.pack()
     
     n = Label(tk,text="Name")
     n.pack()
     E1=Entry(tk,width=40)
     E1.grid(row=1,column=0)
     E1.pack()

     b = Button(tk,width=40)
     b.grid(row=1,column=0)
     

     s = Label(tk,text="Surname")
     E2=Entry(tk,width=40)
     E2.grid(row=1,column=0)
     E2.pack()

     nr = Label(tk,text="Number")
     E3=Entry(tk,width=40)
     E3.grid(row=1,column=0)
     E3.pack()

     

     assert os.path.exists("python.jpg")
     s = SplashScreen(tk, timeout=5000, image="python.jpg")
     mainloop()

Recommended Answers

All 4 Replies

You can't mix geometry managers pack() and grid() withint the same parent widget. I would stick with grid(), now it works:

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

         """
         # you don't need this!!!!!!!!!!!
         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="Phone Book")
     l.grid(row=0, column=0, columnspan=2, pady=5)
     
     n = Label(tk,text="Name")
     n.grid(row=1, column=0, pady=5)
     E1=Entry(tk,width=40)
     E1.grid(row=1, column=1)

     b = Button(tk, text='button', width=40)
     b.grid(row=2,column=0, pady=5)

     s = Label(tk,text="Surname")
     s.grid(row=3,column=0,pady=5)
     E2=Entry(tk,width=40)
     E2.grid(row=3,column=1)

     nr = Label(tk,text="Number")
     nr.grid(row=4,column=0,pady=5)
     E3=Entry(tk,width=40)
     E3.grid(row=4,column=1)


     assert os.path.exists("python.jpg")
     s = SplashScreen(tk, timeout=5000, image="python.jpg")
    
     tk.mainloop()

Also match width and height on your canvas widget!

could please help me on what am i doing wrong

How to verify or check timing of splash screen

commented: Give it a few more years? +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.