Search code examples
pythontkintersplash-screen

splash (loading image) center of screen before main starts


i have a image (400x400) and i want it as splash image (before main program/code is starting) in the center of my screen (1920x1080). My Problem is that i go a black screen/window around my splash image. My Code:

import tkinter as tk
root = tk.Tk()

root.overrideredirect(True)
width = root.winfo_screenwidth()
height= root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width*0.8, height*0.8, width*0.1, height*0.1))
image_file="test.gif"
image = tk.PhotoImage(file=image_file)
canvas = tk.Canvas(root, height=height*0.8, width=width*0.8, bg='black')
canvas.create_image(width*0.8/2, height*0.8/2, image=image)
canvas.pack()

root.after(5000, root.destroy)
root.mainloop()

Black screen around my splash image enter image description here

Here is my splash image

enter image description here

Thanks Guys!


Solution

  • Here is my final answer, that gets the image size and then makes the window as long and wide as the image and then places the window in the center, this does include some simple calculations:

    from tkinter import *
    
    root     = Tk()
    img_file = "test.gif"
    image    = PhotoImage(file=img_file)
    w,h      = image.width(), image.height()
    
    screen_width  = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    
    x = (screen_width  / 2) - (w / 2)
    y = (screen_height / 2) - (h / 2)
    
    root.overrideredirect(True)
    root.geometry(f'{w}x{h}+{int(x)}+{int(y)}')
    
    canvas = Canvas(root, highlightthickness=0)
    canvas.create_image(0,0, image=image, anchor='nw')
    canvas.pack(expand=1,fill='both')
    
    root.after(5000, root.destroy)
    
    root.mainloop()
    

    The problem with using root.eval('tk::PlaceWindow . center') is that it places the top left corner of the window on the center, so the entire app is not centered per se.