Отцентровка и дубляж окна tkinter python
Использую заставку для основного окна в приложении написаного на основе tkinter на пайтоне. Как не нстроивал окно заставки, не могу добиться идеала. Задача чтобы окно затавки повторяло основное окно по размеру и чтобы оба появлялись в центре екрана.
Основное окно:
root = Tk()
root.title('Covid-19 Magic8Ball')
root.resizable(False, False)
canvas = Canvas(root, height= 500, width= 610)
canvas.pack()
Окно заставки:
splash_root = Tk()
splash_root.title("opa")
splash_root.geometry("500x500")
windowWidth = splash_root.winfo_reqwidth()
windowHeight = splash_root.winfo_reqheight()
print("Width",windowWidth,"Height",windowHeight)
# Gets both half the screen width/height and window width/height
positionRight = int(splash_root.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(splash_root.winfo_screenheight()/2 - windowHeight/2)
# Positions the window in the center of the page.
splash_root.geometry("+{}+{}".format(positionRight, positionDown))
splas_label = Label(splash_root, text = "Slash")
splas_label.pack(pady=20)
Ответы (1 шт):
Автор решения: GrAnd
→ Ссылка
from tkinter import *
def center_window(window, parent=None):
window.update_idletasks()
width = window.winfo_width()
height = window.winfo_height()
if parent is None: # центрируем относительно экрана
x = (window.winfo_vrootwidth() - width ) // 2
y = (window.winfo_vrootheight() - height) // 2
else: # центрируем относительно родительского окна
window.wm_transient(parent.winfo_toplevel())
window.lift()
x = parent.winfo_rootx() + (parent.winfo_width() - width ) // 2
y = parent.winfo_rooty() + (parent.winfo_height() - height) // 2
window.geometry(f"+{x}+{y}")
def show_splash(parent):
splash_root = Toplevel()
splash_root.title("opa")
#splash_root.geometry("500x500")
splash_root.geometry(f"{parent.winfo_width()}x{parent.winfo_height()}")
splash_root.overrideredirect(True)
splash_label = Label(splash_root, text = "Splash", bg="#ffa0a0")
splash_label.pack(fill=BOTH, expand=True)
center_window(splash_root, parent)
splash_root.after(2000, lambda: splash_root.destroy()) # автоубирание splash через 2 секунды
root = Tk()
root.title('Covid-19 Magic8Ball')
root.resizable(False, False)
canvas = Canvas(root, height= 500, width= 610)
canvas.pack()
center_window(root)
show_splash(root)
root.mainloop()