如何在Kivy中找到Widget占用的Area/Vertices?
How Can I Find the Area/Vertices Occupied By a Widget in Kivy?
我有一个带有 100 个图像小部件的 10 x 10 GridLayout,并且 on_touch_down,我希望被触摸的图片更改为不同的图片。由于触摸信号会通过 GridLayout 及其所有 100 个 Image 子级冒泡,我想检查 on_touch_down 以查看触摸坐标是否在被触摸的 Image 占据的区域内。我怎样才能找到图像的顶点,或者我的方法有替代方法吗?在添加每个图像时计算它们的四个顶点会相当困难,因为我正在拉伸这些图像。
非常感谢。 :)
self.pos 和 self.size 就足够了。我真傻。
collide_point
方法看起来很适合这个。
例如:
def on_touch_down(self, touch):
x, y = touch
for child in self.children():
if child.collide_point(x, y):
do_something
来自文档:
检查点 (x, y) 是否在小部件的轴对齐边界内
盒子.
:Parameters:
`x`: numeric
X position of the point (in window coordinates)
`y`: numeric
Y position of the point (in window coordinates)
:Returns:
bool, True if the point is inside the bounding box.
..
>>> Widget(pos=(10, 10), size=(50, 50)).collide_point(40, 40)
True
我有一个带有 100 个图像小部件的 10 x 10 GridLayout,并且 on_touch_down,我希望被触摸的图片更改为不同的图片。由于触摸信号会通过 GridLayout 及其所有 100 个 Image 子级冒泡,我想检查 on_touch_down 以查看触摸坐标是否在被触摸的 Image 占据的区域内。我怎样才能找到图像的顶点,或者我的方法有替代方法吗?在添加每个图像时计算它们的四个顶点会相当困难,因为我正在拉伸这些图像。
非常感谢。 :)
self.pos 和 self.size 就足够了。我真傻。
collide_point
方法看起来很适合这个。
例如:
def on_touch_down(self, touch):
x, y = touch
for child in self.children():
if child.collide_point(x, y):
do_something
来自文档:
检查点 (x, y) 是否在小部件的轴对齐边界内 盒子.
:Parameters:
`x`: numeric
X position of the point (in window coordinates)
`y`: numeric
Y position of the point (in window coordinates)
:Returns:
bool, True if the point is inside the bounding box.
..
>>> Widget(pos=(10, 10), size=(50, 50)).collide_point(40, 40)
True