答案 0 :(得分:3)
您可以使用winfo_width
/ winfo_height
(或winfo_reqwidth
,winfo_reqheight
)来获取窗口大小。
def center_window(win):
# win.update_idletasks()
screen_width = win.winfo_screenwidth()
screen_height = win.winfo_screenheight()
width = win.winfo_reqwidth()
height = win.winfo_reqheight()
x = screen_width / 2 - width / 2
y = screen_height / 2 - height / 2
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
使用winfo_reqwidth
,winfo_reqheight
以防窗口未完全设置。
或者您可以在致电winfo_width
/ winfo_height
之前致电update_idletasks
进行几何管理。