窗口定位
居中窗口
# -*- coding:utf8 -*-
from tkinter import *
root = Tk()
root.title("居中的窗口")
screenWidth = root.winfo_screenwidth() # 获取显示区域的宽度
screenHeight = root.winfo_screenheight() # 获取显示区域的高度
width = 300 # 设定窗口宽度
height = 160 # 设定窗口高度
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
# 宽度x高度+x偏移+y偏移
# 在设定宽度和高度的基础上指定窗口相对于屏幕左上角的偏移位置
# root.geometry("%dx%d+%d+%d" % (width, height, left, top))
root.geometry(f"{width}x{height}+{left}+{top}")
root.mainloop()
# 简写代码
root = Tk()
w = 600
h = 300
x = int((screen_width // 2) - (window_width // 2))
y = int((screen_height // 2) - (window_height // 2))
root.geometry(f"{w}x{h}+{x)}+{y}")