splash (loading image) center of screen before main starts splash (loading image) center of screen before main starts tkinter tkinter

splash (loading image) center of screen before main starts


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.


as cool cloud mentioned, it is advisable to set your canvas exactly to the geometry size of 400x400, But you might also have to remove the white highlight around the canvas.. You can achieve that by the following method:-

canvas.config(highlightthickness=0)

Once you have done that you are good to go, but for extra measures,if you still see some leaking canvas in your splash screen, you can do the following:

root.attributes('-transparentcolor',"lime")

This will remove the canvas entirely from being visible to the user.

NOTE:- In order for the above to work, you have to set the colour of your canvas entirely different from any of your color in your image.as this method completely removes the colour from the window and makes it transparent

Hope this works