KIVY DragBehavior自定义小部件

问题描述 投票:0回答:1

我正在尝试使用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()
kivy drag android-relativelayout
1个回答
1
投票

DragBehavior通过调整posMyPaintWidget起作用,但是您已经在pos_hint上设置了MyPaintWidgetpos_hint优先于pos,因此当拖动更改pos时,会因为存在pos_hint而将其忽略。同样,在调用该方法时,在Rectangle中绘制的create_figure会设置其sizepos,并且在移动MyPaintWidget时没有更改它的机制。因此,即使拖动WidgetRectangle也不会移动。

这里是您的代码版本,已纠正了这些问题:

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()
© www.soinside.com 2019 - 2024. All rights reserved.