【问题标题】:Python/Kivy : call function when double click on labelPython/Kivy:双击标签时调用函数
【发布时间】:2018-04-11 05:13:36
【问题描述】:

double点击label时如何调用函数?现在它调用single点击。用什么代替on_ref_press

layout.add_widget(
    MyLabel(text='[ref=world]' + str('Test') + '[/ref]', padding_x=10,
            size_hint_x=.35, halign='left',
            markup=True, on_ref_press=partial(self.xyz, 10)))

【问题讨论】:

    标签: python-2.7 kivy kivy-language


    【解决方案1】:

    一个可能的解决方案是创建事件,在下面的代码中我展示了一个示例:

    from kivy.app import App
    
    from kivy.uix.label import Label
    
    
    class DoubleClickableLabel(Label):
        def __init__(self, **kwargs):
            Label.__init__(self, **kwargs)
            self.register_event_type('on_double_press')
            if kwargs.get("on_double_press") is not None:
                self.bind(on_double_press=kwargs.get("on_double_press"))
    
        def on_touch_down(self, touch):
            if touch.is_double_tap:
                self.dispatch('on_double_press', touch)
                return True
            return Label.on_touch_down(self, touch)
    
        def on_double_press(self, *args):
            pass
    
    
    class MyApp(App):
        def build(self):
            label = DoubleClickableLabel(text='Hello world', on_double_press=self.callback)
            return label
    
        def callback(self, *args):
            print("double clicked", args[0])
    
    
    if __name__ == '__main__':
        MyApp().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 2018-04-22
      相关资源
      最近更新 更多