- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
How to get the Tkinter widget's current x and y coordinates?
Tkinter is widely used to create GUI based applications. It has many toolkits and functions or modules available which can be used to define the different attributes of a particular application. For building GUI applications it provides some widgets including buttons, text boxes and labels. We can customize the position of the widget and its coordinates on the tkinter frame using other functions and libraries.
Let us suppose that we have created a text label widget which is having some position in the tkinter frame. Now, to get the actual coordinates of the widget, we can use the geometry methods available in the tkinter’s library.
We will use winfo_rootx() and winfo_rooty() functions which return the actual coordinates of the widget with respect to the frame or window.
#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") #Define the text-widget my_text= Text(win, height = 5, width = 52) # Create label lab = Label(win, text ="TutorialsPoint.com") #Configure it using other properties lab.config(font =("Helvetica", 20)) #Create a button widget my_button = Button(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 both text widget and button widget widget_x1, widget_y1 = my_button.winfo_rootx(), my_button.winfo_rooty() widget_x2, widget_y2 = my_text.winfo_rootx(), my_button.winfo_rooty() lab.pack() print(widget_x1, widget_y1) print(widget_x2, widget_y2) #Keep the window running win.mainloop()
Running the above code snippet will print the current position of widget as,
134 157 0 157
- Related Articles
- How to get the current length of the Text in a Tkinter Text widget?
- How to get the width of the Tkinter widget?
- How to get the input from the Tkinter Text Widget?
- How to highlight the current line of a Text widget in Tkinter?
- How to insert the current time in an Entry Widget in Tkinter?
- How to get the widget name in the event in Tkinter?
- How to get rid of widget border in Tkinter?
- How to set a widget's size in Tkinter?
- How to change a Tkinter widget's font style without knowing the widget's font family/size?
- How to get the value of an Entry widget in Tkinter?
- How to get coordinates on scrollable canvas in Tkinter?
- How to change the menu background color of Tkinter's OptionMenu widget?
- Tkinter-How to get the current date to display in a tkinter window?
- How to get the coordinates of an object in a Tkinter canvas?
- Get contents of a Tkinter Entry widget