Kivy:在没有 kv 的情况下跟随鼠标移动移动图像 [英] Kivy : Move image following mouse movement without 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_local
、 to_window
、 to_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_down
或 touch_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.
触摸的x
和y
坐标在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屋!