Tkinter:如何在宽度和高度包裹内容的同时固定窗口打开位置

时间:2017-05-12 07:42:15

标签: python tkinter

我希望我的Tkinter窗口在屏幕中央打开,而不必自己输入屏幕的宽度和高度。 我已经通过了this很好的答案,但它需要指定窗口的尺寸。

而不是这个 enter image description here

我想得到这样的结果: enter image description here

1 个答案:

答案 0 :(得分:3)

您可以使用winfo_width / winfo_height(或winfo_reqwidthwinfo_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_reqwidthwinfo_reqheight以防窗口未完全设置。

或者您可以在致电winfo_width / winfo_height之前致电update_idletasks进行几何管理。