splash.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import time
  2. import tkinter as tk
  3. from PIL import Image, ImageTk
  4. class Splash(tk.Toplevel):
  5. def __init__(self, master=None, **kw):
  6. tk.Toplevel.__init__(self, master=master, **kw)
  7. self._root = master
  8. _image = Image.open('img/splash.png')
  9. self._image = ImageTk.PhotoImage(_image)
  10. self._root.withdraw()
  11. tk.Label(self, image=self._image, relief='flat').pack()
  12. self.center()
  13. self.overrideredirect(1)
  14. self.update()
  15. time.sleep(3)
  16. self.destroy()
  17. self._root.deiconify()
  18. def center(self):
  19. """
  20. centers a tkinter window
  21. :param self: the root or Toplevel window to center
  22. """
  23. self.update_idletasks()
  24. width = self.winfo_width()
  25. frm_width = self.winfo_rootx() - self.winfo_x()
  26. win_width = width + 2 * frm_width
  27. height = self.winfo_height()
  28. titlebar_height = self.winfo_rooty() - self.winfo_y()
  29. win_height = height + titlebar_height + frm_width
  30. x = self.winfo_screenwidth() // 2 - win_width // 2
  31. y = self.winfo_screenheight() // 2 - win_height // 2
  32. self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  33. self.deiconify()