【问题标题】:Kivy : Move image following mouse movement without kvKivy:在没有 kv 的情况下跟随鼠标移动移动图像
【发布时间】:2019-02-10 23:35:37
【问题描述】:

我想在鼠标移动后移动图像。 特别是,我不知道如何获得鼠标移动量和平移坐标。

我试过to_localto_windowto_parent,但我没能搞定…… 这是我的最小代码。我应该如何修改?

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()

【问题讨论】:

    标签: kivy


    【解决方案1】:

    代码设置正确。您需要做的是保存鼠标在每个触摸事件中的位置(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)
    

    【讨论】:

    • 成功了!!我误解了这个问题很困难......哈哈。就是这么简单。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多