[英]Get the pixel coordinate of an image in a widget using touch event
My code under class Touchpoint is trying to print x , y co-ordinate of an image in a widget. 我在Touchpoint类下面的代码试图在窗口小部件中打印图像的x,y坐标。 However this code gives coordinates relative to window and not just the image
但是,此代码提供相对于窗口的坐标,而不仅仅是图像
I have tried using collide_point method 我尝试过使用collide_point方法
class TouchPoint(Image):
def on_touch_down(self, touch):
if not self.load_image.collide_point(*touch.pos):
return False
else:print(touch)
TouchPoint:
load_image:load_image
size_hint: 1,.78
pos_hint: {"top": .75, "left":1}
id: load_image
source: 'test_pics/image.png'
This is just created by trial and error, so it mat not be universally correct. 这只是通过反复试验创建的,所以它不是普遍正确的。 But here is an attempt to do what you want:
但这是尝试做你想做的事:
class TouchPoint(Image):
def on_touch_down(self, touch):
if not self.load_image.collide_point(*touch.pos):
return False
else:
# coordinates of image lower left corner inside the TouchPoint widget
im_x = (self.size[0] - self.norm_image_size[0]) / 2.0 + self.x
im_y = (self.size[1] - self.norm_image_size[1]) / 2.0 + self.y
# touch coordinates relative to image location
im_touch_x = touch.x - im_x
im_touch_y = touch.y - im_y
# check if touch is with the actual image
if im_touch_x < 0 or im_touch_x > self.norm_image_size[0]:
print('Missed')
elif im_touch_y < 0 or im_touch_y > self.norm_image_size[1]:
print('Missed')
else:
print('image touch coords:', im_touch_x, im_touch_y)
The norm_image_size
is the actual size of the image in the TouchPoint
. norm_image_size
是TouchPoint
图像的实际大小。 This code assumes that the image will be centered in the TouchPoint
widget. 此代码假定图像将在
TouchPoint
小部件中居中。 No guarantees, but this might give you a starting point. 没有保证,但这可能会给你一个起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.