如何在Kivy找到小部件占用的区域/顶点?

时间:2015-08-07 21:42:41

标签: python python-2.7 user-interface kivy

我有一个10 x 10 GridLayout和100个图像小部件,而on_touch_down,我希望被触摸的图片更改为不同的图片。由于触摸信号将通过GridLayout及其所有100个Image子进行通过,我想检查on_touch_down以查看触摸坐标是否在被触摸的图像占据的区域内。如何找到图像的顶点,还是可以替代我的方法?在添加每个Image的四个顶点时,计算这些顶点将非常困难,因为我正在拉伸这些图像。

非常感谢提前。 :)

2 个答案:

答案 0 :(得分:0)

self.pos和self.size就足够了。我真傻。

答案 1 :(得分:0)

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