Reputation: 9
I just want to know how could I insert a picture into the splash screen?
from tkinter import *
#splash screen
class SplashScreen(Frame):
def __init__(self, master=None, width=0.8, height=0.6, useFactor=True):
Frame.__init__(self, master)
self.pack(side=TOP, fill=BOTH, expand=YES)
# get screen width and height
ws = self.master.winfo_screenwidth()
hs = self.master.winfo_screenheight()
w = (useFactor and ws * width) or width
h = (useFactor and ws * height) or height
# calculate position x, y
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
self.master.overrideredirect(True)
self.lift()
if __name__ == '__main__':
root = Tk()
sp = SplashScreen(root)
sp.config(bg="#3632ff")
m = Label(sp, text="MH60 NAVIGATION APP")
m.pack(side=TOP, expand=YES)
m.config(bg="#3366ff", justify=CENTER, font=("calibri", 29))
Button(sp, text="PRESS TO START", bg='red', command=root.destroy).pack(side=BOTTOM, fill=X)
root.mainloop()
Upvotes: 0
Views: 2656
Reputation: 1
Probably you are trying to use jpg or other type of file, like jpeg..., check this or change the type of file to png.
Upvotes: 0
Reputation: 1
bg = PhotoImage(file = "file_name.png") # insert file name to be display
file_name(photo) should be present(save) in folder where above code is saved.
If file_name is not given in code or the image does not exist in folder where above code is saved then the error will appear while running code.
Error:-
_tkinter.TclError: couldn't open "file_name.png": no such file or directory
Upvotes: 0
Reputation: 1
Step 1:- Import Module.
Step 2:- Create splash screen.
step 3:- Set title and geometry for splash screen.
step 4:- Insert photo file name to be display.
step 5:- Create Label and Pack the Label.
# Import Module
from tkinter import *
splash = Tk()
splash.title("Welcome") # assigning title for splash screen
splash.geometry("800x750+300+100") # set geometry for splash screen
splash.after(4000, splash.destroy) # splash screen will destroy after 4 sec
bg = PhotoImage(file = "file_name.png") # insert file name to be display
lab = Label(splash, image = bg) # create label
lab.pack() # pack the label
splash.mainloop()
Upvotes: 0
Reputation: 123463
Just add some widget with an image to your SplashScreen
instance. For example, say your splash screen image was this .gif
:
Then adding it to your code would look something like this (via a Button
widget):
from tkinter import *
class SplashScreen(Frame):
def __init__(self, master=None, width=0.8, height=0.6, useFactor=True):
Frame.__init__(self, master)
self.pack(side=TOP, fill=BOTH, expand=YES)
# Add widget with the splash screen image on it.
self.img = PhotoImage(file='splash.gif')
btn = Button(self, image=self.img)
btn.pack(expand=YES, ipadx=10, ipady=10)
# get screen width and height
ws = self.master.winfo_screenwidth()
hs = self.master.winfo_screenheight()
w = (useFactor and ws * width) or width
h = (useFactor and ws * height) or height
# calculate position x, y
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
self.master.overrideredirect(True)
self.lift()
if __name__ == '__main__':
root = Tk()
sp = SplashScreen(root)
sp.config(bg="#3632ff")
m = Label(sp, text="MH60 NAVIGATION APP")
m.pack(side=TOP, expand=YES)
m.config(bg="#3366ff", justify=CENTER, font=("calibri", 29))
Button(sp, text="PRESS TO START", bg='red', command=root.destroy).pack(side=BOTTOM, fill=X)
root.mainloop()
This is how it looks running on my system:
Upvotes: 1