krdewra
krdewra

Reputation: 43

Kivy DragBehavior property - simplest way. Why does it not work?

Everything seems to be ok (according to docs) (I know it isnt), but it's impossible to move my Image object.

Image object is visible on BrickCanvas but probably it is untouchable. I tried to print something after on_touch_down event on Image object and after touch down nothing happend.

memo.kv

<BrickCanvas>:
    FloatLayout:
        Brick

    <Brick>:
        drag_rectangle: 100 , 100 , 100 , 100
        drag_timeout: 1000000000000000
        drag_distance: 0
        Image:
            size: (150,150)
            source: '/home/prezes/Desktop/KO.jpg'

main.py

    #!/usr/bin/kivy
    # -*- coding: utf-8 -*-

    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.uix.behaviors import DragBehavior


    class BrickCanvas(Widget):
        pass

    class Brick(DragBehavior,Widget):
        pass


    class MemoApp(App):
        def build(self):
            return BrickCanvas()

    if __name__ == '__main__':
        MemoApp().run()

Upvotes: 2

Views: 304

Answers (1)

Nykakin
Nykakin

Reputation: 8747

Apparently DragBehavior works but only for selected widget and not its children (in this case it's a Image) as you can test with this code:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder

Builder.load_string('''
<BrickCanvas>:
    FloatLayout:
        Brick

<Brick>:
    canvas:
        Color:
            rgb: 0.5, 0.5, 0.5
        Rectangle:
            size: self.size
            pos: self.pos

    Image:
        size: 50, 50
        source: 'test.png' # change to your path
''')

class BrickCanvas(Widget):
    pass

class Brick(DragBehavior,Widget):
    pass


class MemoApp(App):
    def build(self):
        return BrickCanvas()

if __name__ == '__main__':
    MemoApp().run()

You can use Image class directly to avoid this problem:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder

Builder.load_string('''
<BrickCanvas>:
    Brick

<Brick>:
    source: 'test.png' # change to your path
''')

class BrickCanvas(Widget):
    pass

class Brick(DragBehavior, Image):
    pass

class MemoApp(App):
    def build(self):
        return BrickCanvas()

if __name__ == '__main__':
    MemoApp().run()

Still I'd say that for simple drag and drop functionality for an image it's better to just use Scatter widget:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.lang import Builder

Builder.load_string('''
<BrickCanvas>:
    Brick

<Brick>:
    do_scale: False
    do_rotation: False
    Image
        source: 'test.png' # change to your path
''')

class BrickCanvas(Widget):
    pass

class Brick(Scatter):
    pass

class MemoApp(App):
    def build(self):
        return BrickCanvas()

if __name__ == '__main__':
    MemoApp().run()

Upvotes: 1

Related Questions