【问题标题】:Kivy Drag Behavior Labels and RectanglesKivy 拖动行为标签和矩形
【发布时间】:2017-11-26 18:59:17
【问题描述】:

我在一个特定的想法上遇到了麻烦:

我创建了一个可以使用 Kivy 中的拖动行为拖动的矩形。虽然,我希望只向左/向右拖动矩形并固定 Y 位置。

我怎样才能做到这一点?我尝试了多种组合,在网上寻找这个特定的问题,我什至检查了源代码,但我无法把它放在一起。非常感谢您的帮助!

下面是代码:

from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder

kv = '''
<DragLabel>:
    # Define the properties for the DragLabel
    drag_rectangle: self.x, self.y, self.width, self.height
    drag_timeout: 10000000
    drag_distance: 0

FloatLayout:
    # Define the root widget
    DragLabel:
        size_hint: 1.0, 0.2
        text: 'Drag me'
        canvas.before:
            Color:
                rgb: .6, .6, .6
            Rectangle:
                pos: self.pos
                size: self.size 
'''

class DragLabel(DragBehavior, Label):
    pass

class RectangleApp(App):
    def build(self):
        object = Builder.load_string(kv)
        return object

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

【问题讨论】:

    标签: python python-2.7 kivy kivy-language


    【解决方案1】:

    如果你真的想在 DragBehaviour 中使用 Label,那么你必须重新定义 Label 的 on_touch_upon_touch_downon_touch_move

    但实现这一点的更好更简单的方法是使用ScatterLayout 并禁用y 轴上的平移:

    from kivy.app import App
    from kivy.lang import Builder
    
    kv = '''
    FloatLayout:
        # Define the root widget
        ScatterLayout:
            size_hint: 1.0, 0.2
            do_translation_y: False
            Label:
                size_hint: 1.0, 1
                text: 'Drag me'
                canvas.before:
                    Color:
                        rgb: .6, .6, .6
                    Rectangle:
                        pos: self.pos
                        size: self.size 
    '''
    
    class RectangleApp(App):
        def build(self):
            object = Builder.load_string(kv)
            return object
    
    if __name__ == '__main__':
        RectangleApp().run()
    

    【讨论】:

    • 你又成功了!!你太有帮助了,谢谢。
    【解决方案2】:
    from kivy.uix.button import Button
    from kivy.app import App
    from kivy.uix.behaviors import DragBehavior
    from kivy.uix.boxlayout import BoxLayout
    
    
    class Box_layout(BoxLayout):
        def __init__(self,**kwargs):
            super(Box_layout, self).__init__(**kwargs)
            self.size_hint = (.50,.50)
            self.orientation = "vertical"
            for  x in range(5):
               DragButton.text=str(x)
               self.add_widget(DragButton())
    
    class DragButton(DragBehavior, Button):
        def __init__(self,**kwargs):
           super(DragButton, self).__init__(**kwargs)
           self.drag_rect_width = 1366
           self.drag_rect_height = 768
           self.drag_timeout = 10000000
           self.drag_distance = 0
           print(self.height, self.width)
    

    这是 DragBehavior 的另一个示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2018-02-13
      • 2014-10-14
      • 2013-10-02
      相关资源
      最近更新 更多