How to get the Tkinter widget's current x and y coordinates?

Tkinter is widely used to create GUI-based applications. It provides various widgets including buttons, text boxes, and labels. We can customize the position of widgets and retrieve their coordinates on the tkinter frame using geometry methods.

To get the actual coordinates of a widget, we can use the geometry methods available in tkinter's library. The winfo_rootx() and winfo_rooty() functions return the actual coordinates of the widget with respect to the root window.

Syntax

widget.winfo_rootx()  # Returns x-coordinate
widget.winfo_rooty()  # Returns y-coordinate

Example

Here's how to get the current coordinates of different tkinter widgets ?

# Import the tkinter library
from tkinter import *

# Create an instance of the tkinter frame
win = Tk()

# Define the geometry of the frame
win.geometry("600x400")

# Create label
lab = Label(win, text="TutorialsPoint.com")
lab.config(font=("Helvetica", 20))
lab.pack()

# Create a button widget
my_button = Button(win, text="Hello")

# Define the position of the widget
my_button.place(x=100, y=100)

# Update the coordinates with respect to the tkinter frame
win.update()

# Get the coordinates of button widget
widget_x, widget_y = my_button.winfo_rootx(), my_button.winfo_rooty()

print(f"Button coordinates: ({widget_x}, {widget_y})")

# Keep the window running
win.mainloop()

The output of the above code will display the button coordinates ?

Button coordinates: (134, 157)

Getting Coordinates of Multiple Widgets

You can retrieve coordinates of multiple widgets simultaneously ?

from tkinter import *

# Create main window
win = Tk()
win.geometry("500x300")

# Create multiple widgets
label1 = Label(win, text="Label 1", bg="lightblue")
label1.place(x=50, y=50)

label2 = Label(win, text="Label 2", bg="lightgreen")  
label2.place(x=200, y=150)

button = Button(win, text="Click Me")
button.place(x=100, y=200)

# Update window to ensure widgets are positioned
win.update()

# Get coordinates of all widgets
widgets = [
    ("Label 1", label1),
    ("Label 2", label2), 
    ("Button", button)
]

for name, widget in widgets:
    x = widget.winfo_rootx()
    y = widget.winfo_rooty()
    print(f"{name} coordinates: ({x}, {y})")

win.mainloop()
Label 1 coordinates: (84, 107)
Label 2 coordinates: (234, 207)
Button coordinates: (134, 257)

Key Points

  • winfo_rootx() and winfo_rooty() return absolute coordinates relative to the screen
  • Call win.update() before getting coordinates to ensure widgets are properly positioned
  • Coordinates are measured in pixels from the top-left corner of the screen
  • These methods work with all tkinter widgets (Button, Label, Entry, etc.)

Conclusion

Use winfo_rootx() and winfo_rooty() to get the current x and y coordinates of any tkinter widget. Remember to call update() first to ensure accurate positioning information.

Updated on: 2026-03-25T16:54:30+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements