简体   繁体   English

Kivy 中的拖放行为

[英]Drag and drop behaviors in Kivy

I'm new in Python and Kivy.我是 Python 和 Kivy 的新手。

My goal is to drag a button to a certain place (Label), each button and label have their own scores, and if I drag the button and my cursor is inside the label, it will show the tooltip to display the sum of the score of the button and the label, finally if I drop the button on the Label, the button will automatically be on the position the label was, and print the sum of the score.我的目标是将一个按钮拖到某个地方(标签),每个按钮和标签都有自己的分数,如果我拖动按钮并且我的光标在标签内,它会显示工具提示以显示分数的总和按钮和标签,最后如果我将按钮放在标签上,按钮将自动位于标签所在的位置,并打印分数的总和。 However, if I drop the button outside the label, the button will automatically be on the position the button was.但是,如果我将按钮放在标签外,按钮将自动位于按钮所在的位置。

I want to implement some additional functions in DragBehavior:我想在 DragBehavior 中实现一些附加功能:

  • When I drag a button, make it be on the top of the other widgets.当我拖动按钮时,使其位于其他小部件的顶部。

I 've already seen this , but the functions are not enough to me.我已经看到了这个,但功能对我来说还不够。

Here is my .py file:这是我的 .py 文件:

from kivy.app import App
from kivy.properties import StringProperty
from kivy.properties import BooleanProperty
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.behaviors import DragBehavior


class HoverBehavior(object):
    """Hover behavior.
    :Events:
        `on_enter`
            Fired when mouse enter the bbox of the widget.
        `on_leave`
            Fired when the mouse exit the widget
    """

    hovered = BooleanProperty(False)
    border_point = ObjectProperty(None)
    '''Contains the last relevant point received by the Hoverable. This can
    be used in `on_enter` or `on_leave` in order to know where was dispatched the event.
    '''

    def __init__(self, **kwargs):
        self.register_event_type('on_enter')
        self.register_event_type('on_leave')
        Window.bind(mouse_pos=self.on_mouse_pos)
        super(HoverBehavior, self).__init__(**kwargs)

    def on_mouse_pos(self, *args):
        if not self.get_root_window():
            return  # do proceed if I'm not displayed <=> If have no parent
        pos = args[1]
        # Next line to_widget allow to compensate for relative layout
        inside = self.collide_point(*self.to_widget(*pos))
        if self.hovered == inside:
            # We have already done what was needed
            return
        self.border_point = pos
        self.hovered = inside
        if inside:
            self.dispatch('on_enter')
        else:
            self.dispatch('on_leave')

    def on_enter(self):
        pass

    def on_leave(self):
        pass


class DraggableButton(DragBehavior, Button):
    def __init__(self, **kwargs):
        super(DraggableButton, self).__init__(**kwargs)


class DragToHereLabel(HoverBehavior, Label):
    name = StringProperty("")

    def __init__(self, **kwargs):
        super(DragToHereLabel, self).__init__(**kwargs)

    def on_enter(self):
        self.text = "Entered\nScore: 100"

    def on_leave(self):
        self.text = "Leaved\nScore: 100"


class TestApp(App):
    def build(self):
        pass


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

test.kv:测试.kv:

<DraggableButton>:
    drag_rectangle: self.x, self.y, self.width, self.height
    drag_timeout: 10000000
    drag_distance: 0
    halign: "center"


<DragToHereLabel>:
    text: "Drag to here\nScore: 100"
    halign: "center"
    canvas.before:
        Color:
            rgba: 0.2, 0.2, 0.2, 1
        Rectangle:
            pos: self.pos
            size: self.size


BoxLayout:
    orientation: "vertical"

    DraggableButton:
        text: "Drag me\nScore: 200"

    DragToHereLabel:

Thanks a lot for helping a Kivy beginner!非常感谢帮助 Kivy 初学者!

In the on_touch_down() of DraggableButton you can add;DraggableButtonon_touch_down() ,您可以添加;

        parent = self.parent
        parent.remove_widget(self)
        parent.add_widget(self)

This makes sure that the DraggableButton is on top of everything else.这可确保DraggableButton位于其他所有内容之上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM