Kivy:在没有kv的情况下跟随鼠标移动来移动图像 [英] Kivy : Move image following mouse movement without kv

查看:66
本文介绍了Kivy:在没有kv的情况下跟随鼠标移动来移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to move a image following mouse movement. Especially, I'm at a loss how do I get mouse movement amount and translate coordinates.

我尝试了to_localto_windowto_parent,但是我做得不好... 这是我的最小代码.我该如何修改?

I tried to_local , to_window , to_parent but I could not get it well... Here is my minimal code. How should I modify this?

import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image

class TestLayer(FloatLayout):
    def __init__(self, **kwargs):
        super(TestLayer, self).__init__(**kwargs)
        self.pos_hint = {'top':1, 'x':0}
        self.size_hint_x = 1
        self.size_hint_y = 1

    def build(self):
        # specify your image dir and name
        self.img = Image(source='0.png', size=(100, 100))
        self.add_widget(self.img)

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

                # Hold value of touch downed pos
                pass

        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 + (mouse movements distance)
                # self.img.y = self.img.y + (mouse movements distance)
                pass

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

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

                # process after movement or something?
                pass

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

class TestScreen(Screen):
    def __init__(self, **kwargs):
        super(TestScreen, self).__init__(**kwargs)
        layer = TestLayer()
        self.add_widget(layer)
        layer.build()

sm = ScreenManager()

class DemoApp(App):
    def build(self):
        sm.add_widget(TestScreen(name='test'))
        return sm

if __name__ == '__main__':
    DemoApp().run()

推荐答案

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

The code is set up in the correct way. What you need to do is save where the mouse was at each touch event (touch_down or touch_move) and compare to where it is now. Then you take the difference of those two, and move your image by that much.

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

The x and y coordinate of a touch are in the touch.pos[0] and touch.pos[1] variables respectively. For example:

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)

这篇关于Kivy:在没有kv的情况下跟随鼠标移动来移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆