我对touch.grab感到困惑
触摸坐标未转换为窗口小部件空间,因为触摸直接来自窗口。将坐标转换为本地空间是你的工作。
但是当我运行下面的代码时
# -*- coding: utf-8 -*-
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.factory import Factory
class DragRecognizer(Factory.Widget):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
touch.grab(self)
print('on_drag_begin', touch.pos)
return True
else:
return super().on_touch_down(touch)
def on_touch_up(self, touch):
if touch.grab_current is self:
touch.ungrab(self)
print('on_drag_end', touch.pos, '(grab)')
return True
else:
print('on_drag_end', touch.pos)
return super().on_touch_up(touch)
root = Builder.load_string(r'''
<Widget>:
canvas.after:
Line:
rectangle: self.x+2,self.y+2,self.width-3,self.height-3
dash_offset: 5
dash_length: 3
BoxLayout:
Widget:
RelativeLayout
DragRecognizer:
size_hint: 0.8, 0.8
pos_hint: {'center_x': 0.5, 'center_y': 0.5, }
''')
runTouchApp(root)
并按下鼠标按钮。
我期待这样
on_drag_begin (88, 111)
on_drag_end (88, 111)
on_drag_end (488, 111) (grab)
但我得到的是
on_drag_begin (88, 111)
on_drag_end (88, 111)
on_drag_end (88, 111) (grab)
抓取触摸不是窗口坐标!所以我不明白文档的重点。对不起,我的英语不好。如果你帮助我,我真的很感激。
答案 0 :(得分:1)
https://github.com/kivy/kivy/pull/5925
该文档是错误的。抱歉回复太晚。
答案 1 :(得分:0)
您必须使用* instance.to_window( instance.pos)将坐标转换为本地空间。有关详细信息,请参阅示例。
to_window = instance.to_window(*instance.pos)
# -*- coding: utf-8 -*-
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.properties import ListProperty
class DragRecognizer(Factory.Widget):
pressed = ListProperty([0, 0])
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
touch.grab(self)
self.pressed = touch.pos
print('on_touch_down: on_drag_begin', touch.pos)
return True
def on_touch_up(self, touch):
if touch.grab_current is self:
touch.ungrab(self)
print('on_touch_up: on_drag_end', touch.pos, '(grab)')
return True
def on_pressed(self, instance, pos):
print("Pressed at {}".format(pos))
to_window = instance.to_window(*instance.pos)
print("to_window = {}".format(to_window))
to_widget = instance.to_widget(*to_window)
print("to_widget = {}".format(to_widget))
root = Builder.load_string(r'''
<Widget>:
canvas.after:
Line:
rectangle: self.x+2,self.y+2,self.width-3,self.height-3
dash_offset: 5
dash_length: 3
BoxLayout:
Widget:
RelativeLayout
DragRecognizer:
size_hint: 0.8, 0.8
pos_hint: {'center_x': 0.5, 'center_y': 0.5, }
''')
runTouchApp(root)