How can I determine the position of a Toplevel in Tkinter?

In Tkinter, you can determine the position of a Toplevel window using the winfo_x() and winfo_y() methods. These methods return the x and y coordinates of the window relative to the screen.

Methods to Get Toplevel Position

Tkinter provides several methods to retrieve window position information:

  • winfo_x() ? Returns the x-coordinate of the top-left corner

  • winfo_y() ? Returns the y-coordinate of the top-left corner

  • winfo_rootx() ? Returns absolute x-coordinate on screen

  • winfo_rooty() ? Returns absolute y-coordinate on screen

Example

Here's how to create a Toplevel window and get its position coordinates ?

# Import the Tkinter library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter frame
win = Tk()

# Define the geometry
win.geometry("750x350")
win.title("Main Window")

def toplevel_position():
    x_pos = top.winfo_x()
    y_pos = top.winfo_y()
    print(f"The coordinates of Toplevel window are: {x_pos}, {y_pos}")
    
    # Also get root coordinates
    root_x = top.winfo_rootx()
    root_y = top.winfo_rooty()
    print(f"Root coordinates: {root_x}, {root_y}")

# Create Toplevel window
top = Toplevel(win, height=150, width=300)
top.title("This is the Toplevel Window")
top.attributes('-topmost', 'true')

# Create button to get position
button = ttk.Button(top, text="Get Position", command=toplevel_position)
button.place(relx=0.5, rely=0.5, anchor=CENTER)

# Start the main loop
win.mainloop()

Output

When you run this code, a main window will appear with a Toplevel window on top. Click the "Get Position" button to see the coordinates ?

The coordinates of Toplevel window are: 282, 105
Root coordinates: 282, 105

Setting Toplevel Position

You can also set the position of a Toplevel window using the geometry() method ?

from tkinter import *

root = Tk()
root.geometry("400x300")

# Create Toplevel at specific position
top = Toplevel(root)
top.geometry("200x150+100+50")  # width x height + x_offset + y_offset
top.title("Positioned Toplevel")

# Get and display the position
def show_position():
    print(f"Position: ({top.winfo_x()}, {top.winfo_y()})")

Button(top, text="Show Position", command=show_position).pack(pady=20)

root.mainloop()
Position: (100, 50)

Conclusion

Use winfo_x() and winfo_y() methods to get the current position of a Toplevel window. You can set the initial position using the geometry string format "widthxheight+x+y".

Updated on: 2026-03-26T18:30:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements