Tom Boy
Tom Boy

Reputation: 659

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.

Upvotes: 7

Views: 16605

Answers (2)

Kenly
Kenly

Reputation: 26698

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.

Upvotes: 12

Rok Povsic
Rok Povsic

Reputation: 4925

You can skip getting/setting the dialog width/height and set only its X, Y position:

x = root.winfo_x()
y = root.winfo_y()
toplevel.geometry("+%d+%d" % (x + 100, y + 200)))

Replace 100 and 200 with relative X, Y to your root window.

Upvotes: 8

Related Questions