Python tkinter如何把窗口在屏幕中央居中
# -*- coding: utf-8 -*-
import tkinter as tk
# 创建 Tkinter 窗口
root = tk.Tk()
# 设置窗口大小
root.geometry("400x300")
# 获取屏幕宽度和高度
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
# 计算窗口显示时的左上角坐标
centerX = int((screenWidth - root.winfo_reqwidth()) / 2)
centerY = int((screenHeight - root.winfo_reqheight()) / 2)
# 设置窗口显示时的左上角坐标
root.geometry("+{}+{}".format(centerX , centerY ))
# main function
if __name__ == '__main__':
# 窗口主循环
root.mainloop()