How to create a Splash Screen in Python Tkinter

A Splash screen is a graphical control element that is typically displayed when an application is launching (usually as a placeholder while the application is loading). In this Python tutorial we will explore how to create a Splash Screen in Tkinter.

Note: It doesn’t matter whether you are developing a Tkinter application or not. The splash screen will be completely independent of that, and you can use it in non-tkinter applications.


Creating Splash Screens in Tkinter

The below Code creates the root window using the Tk() method and immediately hides it using the withdraw() method. Calling Tk() opens up a blank window automatically. We want to hide this until it is fully loaded. The Splash screen will be displayed using a separate Toplevel widget.

import tkinter as tk
import time

root = tk.Tk()
root.withdraw()

# SPLASH SCREEN CODE
splash_screen = tk.Toplevel(background="white")
splash_screen.overrideredirect(True)
splash_screen.title("Splash Screen")

The overrideredirect method is used to disable the window manager’s control over the splash screen, which allows the splash screen to be displayed without the window manager’s window decorations (such as the title bar and border).

Now what we want to do is center our Splash Screen right in the middle. We will define a special function for this which calculates the x and y coordinates where we should begin our window from. These coordinates will be placed in the tkinter geometry function.

def centerWindow(width, height, root):  # Return 4 values needed to center Window
    screen_width = root.winfo_screenwidth()  # Width of the screen
    screen_height = root.winfo_screenheight() # Height of the screen     
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    return int(x), int(y)

x, y = centerWindow(400, 300, root)
splash_screen.geometry(f"400x300+{x}+{y}")

If you want to learn more about how the above code works, check this tutorial on Window centering.

Next we will load an image for our Splash Screen into a Label widget. We will also call the update() method on our Splash Screen. This is really important to do, otherwise the splash screen will not render until mainloop() is called.

image = tk.PhotoImage(file="crown.png") 
label = tk.Label(splash_screen, image = image)
label.pack()

splash_screen.update()

If you have any trouble with loading the image, it might be because of an unsupported format (even png files can sometimes get rejected). Instead, you can use Pillow for loading images.

Now we have our main code. All the heavy processing and loading stuff goes here.

# MAIN WINDOW CODE + Other Processing
time.sleep(3)

# Start the event loop
root.deiconify()
splash_screen.destroy()
root.mainloop()

The deiconify() method reverses the withdraw() method that we used earlier. We also need to destroy the splash screen now that the main window has been brought back (fully loaded this time) using the destroy method.

Finally we call mainloop to begin our code.

Note: If you are not building a Tkinter application, remove the last and third-last lines.


Tkinter Splash Screen Code

Here is the complete compiled code.

import tkinter as tk
import time

def centerWindow(width, height, root):  # Return 4 values needed to center Window
    screen_width = root.winfo_screenwidth()  # Width of the screen
    screen_height = root.winfo_screenheight() # Height of the screen     
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    return int(x), int(y)

root = tk.Tk()
root.withdraw()

# SPLASH SCREEN CODE
splash_screen = tk.Toplevel(background="white")
splash_screen.overrideredirect(True)
splash_screen.title("Splash Screen")
x, y = centerWindow(400, 300, root)
splash_screen.geometry(f"400x300+{x}+{y}")

image = tk.PhotoImage(file="crown.png") 
label = tk.Label(splash_screen, image = image)
label.pack()
splash_screen.update()

# MAIN WINDOW CODE + Other Processing
time.sleep(3)

# Start the event loop
root.deiconify()
splash_screen.destroy()
root.mainloop()

This marks the end of the How to create a Splash Screen in Python Tkinter Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions about the tutorial content can be asked in the comments section below.

3 thoughts on “How to create a Splash Screen in Python Tkinter”

  1. really enjoyable :))

    Can I also change condition ?
    for example here we used time.sleep(3) , but I have a Program scraping data from a web page and wanna see loading page untill all data gets received

    thank for your efficient blog

    Reply

Leave a Comment