【问题标题】:Get the pixel coordinate of an image in a widget using touch event使用触摸事件获取小部件中图像的像素坐标
【发布时间】:2019-06-21 14:59:27
【问题描述】:

我在 Touchpoint 类下的代码正在尝试打印小部件中图像的 x 、 y 坐标。然而,这段代码给出了相对于窗口的坐标,而不仅仅是图像

我尝试过使用collide_point方法

片段 - py

​​>
class TouchPoint(Image):
    def on_touch_down(self, touch):
        if not self.load_image.collide_point(*touch.pos):
            return False
        else:print(touch)

片段 - kv 文件

                TouchPoint:
                    load_image:load_image
                    size_hint: 1,.78
                    pos_hint: {"top": .75, "left":1}
                    id: load_image
                    source: 'test_pics/image.png'

【问题讨论】:

  • 请注意,'left' 不是 pos_hint 字典的有效键。
  • 您正在打印touch 事件,其中包含鼠标单击位置的坐标。那是你要的吗?请说明您要完成的工作。
  • 嘿约翰我想要我点击的图像上像素的确切 x,y 坐标但是我猜你提到这只是给我鼠标点击位置相对于窗口的任何想法我该如何完成这。感谢您的支持

标签: python image position kivy pixel


【解决方案1】:

这只是通过反复试验产生的,因此它不一定是普遍正确的。但这里尝试做你想做的事:

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)

norm_image_sizeTouchPoint 中图像的实际大小。此代码假定图像将在 TouchPoint 小部件中居中。不能保证,但这可能会给你一个起点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2011-03-28
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多