tkinter屏幕居中显示

"""
   tkinter居中显示代码
"""

import tkinter as tk

def center(win):
    """
       居中显示函数
    """
    win.update_idletasks()
    width = win.winfo_width()   # 自己的宽度
    height = win.winfo_height() # 自己的高度
    # 下面是获取屏幕中心点距离左上角win宽高分别一半时的那个坐标.
    x = (win.winfo_screenwidth() // 2) - (width // 2)
    y = (win.winfo_screenheight() // 2) - (height // 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))

if __name__ == '__main__':
    
    root = tk.Tk()
    root.title("tkinter屏幕居中显示")
    center(root) 
    root.mainloop()