Search code examples
pythonkivykivy-language

Python kivy dragging boundaries


I'm making an App in python kivy, and I have a problem. I'm dragging an image and even If I hold outside the image it still drags. I want the image to drag only if I'm holding on the image. I tried to fix the problem but couldn't find any solution. Below is my code! Any help is appreciated! Thank You!

This is my .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"
'''

This is my 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)

Solution

  • You can use the actual size of the picture that you see instead of the widget size when calculating the 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