pythonpython-3.xtkinter

How to position topLevel() widget relative to root window?


I have created a topLevel widget and was wondering if there was a way to position the new window relative to the root window.


Solution

  • Get root window position:

    x = root.winfo_x()
    y = root.winfo_y()
    

    Use geometry to set the position:

    w = toplevel.winfo_width()
    h = toplevel.winfo_height()  
    toplevel.geometry("%dx%d+%d+%d" % (w, h, x + dx, y + dy))
    

    where dx and dy are the offsets from the current position.