← Volver

Cómo centrar la ventana en Tk (tkinter)

import tkinter as tk

def center_window(window):
"""
Basado en 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()
# Centrar la ventana.
center_window(root)
root.mainloop()


tkinter ventana aplicaciones-de-escritorio


👨‍💻 ¡Ya lanzamos el curso oficial de Recursos Python en Udemy! Un curso moderno para aprender Python desde cero con programación orientada a objetos, SQL y tkinter en 2024.

🐍 También te puede interesar:


💡 Ofrecemos servicios profesionales de desarrollo y capacitación en Python a personas y empresas. Consultanos por tu proyecto.