简体   繁体   中英

Getting the coordinates of the button in Tkinter

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

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 .

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM