禁用 DragBehavior 自定义小部件

KIVY DragBehavior Custom Widget

我正在尝试使用 DragBehavior 来帮助在 RelativeLayout 中移动我的自定义小部件。找到下面的示例代码。为什么我的小部件没有移动 请拖动操作。为简单起见,我在我的自定义小部件 MyPaintWidget

中只包含了矩形
from kivy.app import App
from kivy.graphics import Line
from kivy.uix.scatter import Scatter
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder
from kivy.graphics import Color, Rectangle

Builder.load_string("""
<MyPaintWidget>:
    # Define the properties for the DragLabel
    drag_rectangle: self.x, self.y, self.width, self.height
    drag_timeout: 10000000
    drag_distance: 0
""")


class MyPaintWidget(DragBehavior, Scatter):

    def __init__(self, **kwargs) :
        self.selected = None
        self.touched = False
        super(MyPaintWidget, self).__init__(**kwargs)

    def create_figure(self,  **kwargs):
        print ('position is {}'.format(self.pos))
        print ('width Height {}'.format(self.to_parent(self.width, self.height)))
        self.canvas.add(Rectangle(pos  = self.pos, size = self.size))
        return self

    def on_touch_move(self, touch):
        print('Started to move x: {} y: {}'.format(touch.x, touch.y))
        return super(MyPaintWidget, self).on_touch_move(touch)


class MyPaintApp(App):

    def build(self):
        parent = RelativeLayout()
        self.painter = MyPaintWidget(pos_hint={"center_x": 0.5, 'center_y':0.5}, size_hint=(.2,.1))

        parent.add_widget(self.painter.create_figure())
        return parent

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

DragBehavior 通过调整 MyPaintWidgetpos 来工作,但是您已经在 MyPaintWidget 上设置了 pos_hintpos_hint 优先于 pos,因此当拖动更改 pos 时,它会被忽略,因为存在 pos_hint。此外,您在 create_figure 中绘制的 Rectangle 在调用该方法时设置了 sizepos,并且在 MyPaintWidget 被移动。所以,即使 Widget 被拖动, Rectangle 也不会移动。

这是您的代码版本,其中的问题已更正:

from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder

Builder.load_string("""
<MyPaintWidget>:
    # Define the properties for the DragLabel
    drag_rectangle: self.x, self.y, self.width, self.height
    drag_timeout: 10000000
    drag_distance: 0
    canvas:
        Color:
            rgba: 1,0,0,1
        Rectangle:
            pos: 0,0  # only do this for RelativeLayout
            size: self.size
""")


class MyPaintWidget(DragBehavior, Scatter):

    def __init__(self, **kwargs) :
        self.selected = None
        self.touched = False
        super(MyPaintWidget, self).__init__(**kwargs)

    def on_touch_move(self, touch):
        print('Started to move x: {} y: {}'.format(touch.x, touch.y))
        return super(MyPaintWidget, self).on_touch_move(touch)


class MyPaintApp(App):

    def build(self):
        parent = RelativeLayout()
        self.painter = MyPaintWidget( pos=(240, 200), size_hint=(.2,.1))

        parent.add_widget(self.painter)
        return parent

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