【问题标题】:Python kivy dragging boundariesPython kivy 拖拽边界
【发布时间】:2022-01-02 22:48:13
【问题描述】:

我正在用 python kivy 制作一个应用程序,但我遇到了问题。我正在拖动图像,即使我将图像放在图像之外,它仍然会拖动。我希望仅在按住图像时才拖动图像。我试图解决这个问题,但找不到任何解决方案。下面是我的代码!任何帮助表示赞赏!谢谢!

这是我的.kv

kv = '''

<DragImage>:
    drag_rectangle: self.x+self.width/3, self.y+self.height/3, self.width/3, self.height/3
    drag_timeout: 10000000
    drag_distance: 0


<MainScreen>:
#:import utils kivy.utils
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "Background.png"

    DragImage
        id: book
        pos: 0, 102
        size_hint: 1, .1
        source: "Tv.png"
'''

这是我的 main.py

    class DragImage(DragBehavior, Image):
        def on_touch_up(self, touch):
        uid = self._get_uid()
        if uid in touch.ud:
            print(self.source, 'dropped at', touch.x, touch.y)
        return super(DragImage, self).on_touch_up(touch)

【问题讨论】:

  • 请记住,Image 小部件可能比您看到的图片大。 size_hint: 1 表示它将是您的MainScreen 的整个宽度。

标签: python kivy kivy-language


【解决方案1】:

在计算drag_rectangle时,您可以使用您看到的图片的实际大小而不是小部件大小:

<DragImage>:
    drag_rectangle: self.center[0] - self.norm_image_size[0]/6, self.center[1] - self.norm_image_size[1]/6, \
        self.norm_image_size[0]/3, self.norm_image_size[1]/3

【讨论】:

  • 很有效,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多