Placing child window relative to parent in Tkinter python

一世执手 提交于 2019-12-04 11:07:55

The short answer is, use winfo_rootx and winfo_rooty to get the coordinates relative to the screen. And yes, wm_geometry is the way to place a toplevel window precisely.

For example:

    x = parentWgdt.winfo_rootx()
    y = parentWgdt.winfo_rooty()
    height = parentWgdt.winfo_height()
    geom = "+%d+%d" % (x,y+height)

As a bit of friendly advice, I recommend against abbrev var nms. It makes the code hard to read, especially when the abbreviation is wrong (Wgdt should at least be Wdgt). The difference in code size between geom and geometry, and Wgdt and Widget are tiny, but the difference in readability is huge.

Gérard LAINÉ

According to Tk manual "https://www.tcl.tk/man/tcl8.4/TkCmd/winfo.htm#M52" If you need the true width immediately after creating a widget, invoke update to force the geometry manager to arrange it, or use winfo reqwidth to get the window's requested width instead of its actual width.

# This code works perfectly
self.update()

self.geometry("+%d+%d" % (self.parent.winfo_rootx()+50,
                          self.parent.winfo_rooty()+50
                         )
             ) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!