Mar*_*ito 5 python grid tkinter python-3.x
我正在尝试使用Tkinter中grid_location的网格几何管理器中的方法,但似乎我做错了.
这是我的代码:
from tkinter import *
root = Tk()
b=Button(root, text="00")
b.grid(row=0, column=0)
b2=Button(root, text="11")
b2.grid(row=1, column=1)
b3=Button(root, text="22")
b3.grid(row=2, column=2)
b4=Button(root, text="33")
b4.grid(row=3, column=3)
b5=Button(root, text="44")
b5.grid(row=4, column=4)
def mouse(event):
print(event.x, event.y)
print(root.grid_location(event.x, event.y))
root.bind("<Button-1>", mouse)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
当我在按钮外面单击时,它可以工作,但是当我在任何按钮内部单击时,似乎每个按钮都有自己的坐标系.因此,每个按钮都在(0,0)单元格上,尽管在代码中,它们位于常规网格上.
你是对的,每个按钮"都有它自己的坐标系".但更准确地说,event.x和event.y值是相对于与事件关联的窗口小部件而不是窗口小部件的父窗口或根窗口.
如果确实需要窗口小部件所在的行和列,则可以使用它grid_info来获取与事件关联的窗口小部件的行和列.例如:
def mouse(event):
grid_info = event.widget.grid_info()
print("row:", grid_info["row"], "column:", grid_info["column"])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4283 次 |
| 最近记录: |