Search code examples
pythontkintertkinter-canvas

How to get the x and y coordinates of a label in TKINTER?


I have created a Tkinter form and have added a label on it and placed it at (x=10,y=15) using the following command:

label_0.place(x=10,y=15)

what I'd like to do is something like:

print(label_0.place)

expected output:

x=10,y=15

How can I print the coordinates? Thanks.


Solution

  • The method place_info will return all of the information used by place for that widget.

    >>> print(label_0.place_info())
    {'in': <tkinter.Tk object .>, 'x': '10', 'relx': '0', 'y': '15', 'rely': '0', 'width': '', 'relwidth': '', 'height': '', 'relheight': '', 'anchor': 'nw', 'bordermode': 'inside'}
    

    If you just want the x/y coordinate no matter how it was added to the window, you can use the winfo_x() and winfo_y() methods.