Kivy DragBehavior属性 - 最简单的方法。为什么它不起作用?

时间:2015-10-23 19:50:09

标签: python kivy

一切似乎都没问题(根据文档)(我知道它不是),但是移动我的Image对象是不可能的。

图像对象在BrickCanvas上可见,但可能是不可触及的。我试图在Image对象上的on_touch_down事件之后打印一些东西,并在触摸后没有任何事情发生。

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()

1 个答案:

答案 0 :(得分:1)

显然id有效,但仅适用于选定的小部件而不是其子级(在本例中为DragBehavior),因为您可以使用此代码进行测试:

Image

您可以直接使用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() 课程来避免此问题:

Image

我仍然会说,对于图像的简单拖放功能,最好只使用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() 小部件:

Scatter