如何相对于根窗口定位topLevel()小部件?

Tom*_*Boy 3 python tkinter python-3.x

我创建了一个topLevel小部件,想知道是否存在一种相对于根窗口定位新窗口的方法。

ype*_*per 8

您可以跳过获取/设置对话框宽度/高度并仅设置其 X、Y 位置:

x = root.winfo_x()
y = root.winfo_y()
toplevel.geometry("+%d+%d" % (x + 100, y + 200)))
Run Code Online (Sandbox Code Playgroud)

将 100 和 200 替换为相对于根窗口的 X、Y。


Ken*_*nly 6

获取root窗口位置:

x = root.winfo_x()
y = root.winfo_y()
Run Code Online (Sandbox Code Playgroud)

使用geometry设置的位置:

w = toplevel.winfo_width()
h = toplevel.winfo_height()  
toplevel.geometry("%dx%d+%d+%d" % (w, h, x + dx, y + dy)))
Run Code Online (Sandbox Code Playgroud)

编辑

其中dxdy是当前位置的偏移量。