【问题标题】:Python/Kivy : Can i call function on double click on LabelPython/Kivy:我可以在双击标签时调用函数吗
【发布时间】:2018-03-13 10:03:53
【问题描述】:

谁能告诉我如何在双击标签Item1,Item2时调用函数def demo()

test.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen

def convert_data(data):
    l = []
    for item in data:
        for key, value in item.items():
            l.append({'text': key, 'value': value})
    return l

class test():

    def demo(self):
        print('Demo')

class Invoice(Screen):
    def abc(self):
        arr = ({'Item1': ''},{'Item2': 1000})

        self.rv.data = convert_data(arr)

class MyApp(App):
    def build(self):
        return Builder.load_file('test.kv')

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

test.kv

<Button@Button>:
    font_size: 15
    size_hint_y:None
    height: 30

<Label@Label>:
    font_size: 15
    size_hint_y:None
    height: 30

<Item@GridLayout>:
    cols: 2
    text: ""
    value: 0
    padding : 5, 0
    spacing: 10, 0
    Label:
        size_hint_x: .35
        text: root.text
        halign: 'left'
        valign: 'middle'
        canvas.before:
            Color:
                rgb: .6, .6, .6
            Rectangle:
                pos: self.pos
                size: self.size

    Label:
        size_hint_x: .15
        text: str(root.value)
        halign: 'right'
        valign: 'middle'
        canvas.before:
            Color:
                rgb: .6, .6, .6
            Rectangle:
                pos: self.pos
                size: self.size

Invoice:
    rv: rv
    BoxLayout:
        orientation: "vertical"
        padding : 15, 15

        BoxLayout:
            orientation: "vertical"
            padding : 5, 5
            size_hint: .6, None
            pos_hint: {'x': .18,}


            BoxLayout:
                orientation: "horizontal"
                padding : 5, 5
                spacing: 10, 10
                size: 800, 40
                size_hint: 1, None

                Button:
                    text: "Show"
                    size_hint_x: .05
                    spacing_x: 30
                    on_press:root.abc()

        BoxLayout:
            orientation: "horizontal"
            size_hint: 1, 1

            BoxLayout:
                orientation: "vertical"
                size_hint: .5, 1
                padding : 0, 15
                spacing: 10, 10
                size: 500, 30


                BoxLayout:
                    RecycleView:
                        id: rv
                        viewclass: 'Item'
                        RecycleBoxLayout:
                            default_size: None, dp(30)
                            default_size_hint: 1, None
                            size_hint_y: None
                            height: self.minimum_height
                            orientation: 'vertical'

【问题讨论】:

    标签: python python-2.7 kivy kivy-language


    【解决方案1】:

    您可以创建 Label 的子类并将 double_tap 事件附加到它

    class MyLabel(Label):
        def on_touch_down(self, touch):
            if touch.is_double_tap:
                demo()
    

    【讨论】:

    • 你能再帮我一个忙吗?当我双击(touch.is_double_tap:)然后它会多次调用demo()函数。如何只调用一次?
    • 请参阅文档了解如何实现其他功能,您可以通过在参数中添加double_tap_time = 1 来做到这一点
    • @Joeydanieldarko - 您好,我将您的代码复制/粘贴到我正在使用的文件中。我发现的一个问题是,无论我在屏幕上的哪个位置双击,都会调用 demo()。我错过了什么吗?
    • 我猜你必须查看触摸的 collide_points 并在需要的父对象上运行 demo()
    猜你喜欢
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2021-06-07
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多