[英]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
.当我点击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_*
方法将为您提供有关 window 的信息。例如, winfo_rootx
和winfo_rooty
将返回小部件左上角的屏幕(而非窗口)坐标。 winfo_x
and winfo_y
will return the coordinate of the widget relative to its parent. winfo_x
和winfo_y
将返回小部件相对于其父级的坐标。 You can get the width and height with winfo_width
and winfo_height
.您可以使用winfo_width
和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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.