【问题标题】:How to callback and print the text of a MDLabel when double pressed on Kivy?在 Kivy 上双击时如何回调和打印 MDLabel 的文本?
【发布时间】:2023-03-09 02:46:01
【问题描述】:

我有下面与 Kivy 一起使用的当前 python 代码:

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

####IMPORTANT CODES START FROM HERE:
class of a Screen:

  def of something:
    query_all= s.query(Invoice).all()
    for data in query_all:
        trading_partner_name= DoubleClickableLabel(text=str(data.trading_partner_name), halign="center", on_double_press=self.callback,color=(0,0,1,1))

  def callback(self, *args):
     print("double clicked", *args)

在回调函数(def回调)中,如何从双击标签中获取DoubleClickableLabel(str(data.trading_partner_name))的文本值并打印出来?

目前,如果我在标签上双击,它会打印 *args,这只是一些随机屏幕字符 东西...

我是一名从事计算机科学项目的学生,正在努力解决这个问题....如果您能帮助我,我将不胜感激!

谢谢!

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    下次请给出一个可行的例子。

    from kivy.app import App
    from kivy.uix.label import Label
    
    
    class DoubleClickableLabel(Label):
        def on_touch_down(self, touch):
            if touch.is_double_tap:
                print("you double clicked on me and my text is:",self.text)
    
    
    class MyFirstKivyApp(App):
        def build(self):
            return DoubleClickableLabel(text="Hello World !")
    
    
    MyFirstKivyApp().run()
    

    PS:如果这是您正在寻找的答案,那么您的问题与Python/Kivy : Can i call function on double click on Label 重复。

    【讨论】:

    • 它没有用...但我感谢您的帮助。谢谢!
    • 你的意思是,事件没有被调用?该事件被调用但它没有进入 if 条件?您有特定的错误消息?
    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多