gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:14 27 4
gpt4 key购买 nike

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

而不是这个 enter image description here

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

最佳答案

您可以使用 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_reqwidthwinfo_reqheight 以防窗口未完全设置。

或者您可以调用update_idletasks在调用winfo_width/winfo_height 进行几何管理之前

关于python - Tkinter:如何在让宽度和高度包裹内容的同时固定窗口打开位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43932106/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com