splash.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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('gui/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.lift()
  14. self.attributes('-topmost', True)
  15. self.attributes('-topmost', False)
  16. self.overrideredirect(1)
  17. self.update()
  18. time.sleep(3)
  19. self.destroy()
  20. self._root.deiconify()
  21. def center(self):
  22. """
  23. centers a tkinter window
  24. :param self: the root or Toplevel window to center
  25. """
  26. self.update_idletasks()
  27. width = self.winfo_width()
  28. frm_width = self.winfo_rootx() - self.winfo_x()
  29. win_width = width + 2 * frm_width
  30. height = self.winfo_height()
  31. titlebar_height = self.winfo_rooty() - self.winfo_y()
  32. win_height = height + titlebar_height + frm_width
  33. x = self.winfo_screenwidth() // 2 - win_width // 2
  34. y = self.winfo_screenheight() // 2 - win_height // 2
  35. self.geometry('{}x{}+{}+{}'.format(width, height, x, y))
  36. self.deiconify()