Kivy:在没有kv的情况下跟随鼠标移动图像

时间:2019-02-10 23:35:38

标签: kivy

我想跟随鼠标移动图像。 特别是,我不知如何获得鼠标移动量并转换坐标。

我尝试了def orb_calc_matches(matches, distance_range=0.65): good_matches = [] queried_matches = set() for m in matches: if len(m) == 2: if ((m[0].trainIdx not in queried_matches) and (m[0].distance < distance_range * m[1].distance)): good_matches.append(m[0]) queried_matches.add(m[0].trainIdx) return good_matches to_localto_window,但是我做得不好... 这是我的最小代码。我该如何修改?

to_parent

1 个答案:

答案 0 :(得分:0)

代码以正确的方式设置。您需要做的是保存每次触摸事件(touch_downtouch_move)上鼠标的位置,并与现在的位置进行比较。然后,您将这两者的差值进行调整,并将图像移动那么多。

触摸的xy坐标分别在touch.pos[0]touch.pos[1]变量中。 例如:

def on_touch_down(self, touch):
    if self.collide_point(*touch.pos):
        if touch.button == 'left':

            # Hold value of touch downed pos
            self.last_touch = touch.pos # Need this line
    return super(TestLayer, self).on_touch_down(touch)

def on_touch_move(self, touch):
    if self.collide_point(*touch.pos):
        if touch.button == 'left':
            self.img.x = self.img.x + touch.pos[0] - self.last_touch[0] # Add the x distance between this mouse event and the last
            self.img.y = self.img.y + touch.pos[1] - self.last_touch[1] # Add the y distance between this mouse event and the last
            self.last_touch = touch.pos # Update the last position of the mouse

    return super(TestLayer, self).on_touch_move(touch)