← Go Back
How to Center Window in Tk (tkinter)
import tkinter as tk
def center_window(window):
"""
Based on https://stackoverflow.com/a/10018670.
"""
window.update_idletasks()
width = window.winfo_width()
frm_width = window.winfo_rootx() - window.winfo_x()
win_width = width + 2*frm_width
height = window.winfo_height()
titlebar_height = window.winfo_rooty() - window.winfo_y()
win_height = height + titlebar_height + frm_width
x = window.winfo_screenwidth()//2 - win_width//2
y = window.winfo_screenheight()//2 - win_height//2
window.geometry('{}x{}+{}+{}'.format(width, height, x, y))
window.deiconify()
root = tk.Tk()
center_window(root)
root.mainloop()
🐍 You might also find interesting: