Home > other >  Getting the coordinates of the button in Tkinter
Getting the coordinates of the button in Tkinter

Time:01-10

I want to get the coordinates of the button when I click on the button in tkinter. How can I do this? Please help.

CodePudding user response:

The winfo_* methods will give you information about a window. For example, winfo_rootx and winfo_rooty will return the screen (not window) coordinate of the upper left corner of the widget. winfo_x and winfo_y will return the coordinate of the widget relative to its parent. You can get the width and height with winfo_width and winfo_height.

CodePudding user response:

try this small example.

import tkinter as tk

root = tk.Tk()

btn = tk.Button(root, text=str(1))
btn.pack(padx=10, pady=10)

root.update()

widget_x, widget_y = btn.winfo_rootx(), btn.winfo_rooty()
print(f'{widget_x}, {widget_y}')

root.mainloop()
  • Related