【问题标题】:How to access the value for text of a button upon pressing it. [KIVY]如何在按下按钮时访问按钮文本的值。 [基维]
【发布时间】:2021-03-06 17:13:44
【问题描述】:

我试图在按下 python 中的按钮时访问文本的值。

class MainScreen(Screen):
    def get_text_value():
        *something to get text value of button*        
    pass

<MainScreen>
  Button:
    text: 'Example'
    on_release: root.get_text_value()

在此示例中,我希望能够从 Python 访问字符串“示例”。 如果有意义,函数 get_text_value 将返回字符串“示例”。

非常感谢您的帮助!非常感谢。

【问题讨论】:

  • 您可以为按钮设置id并使用self.root.ids.btn.text或将值传递给函数root.get_text_value(self.text)

标签: python python-3.x kivy kivy-language


【解决方案1】:

您可以通过在kv 中明确指定文本来将文本传递给get_text_value() 方法:

<MainScreen>
  Button:
    text: 'Example'
    on_release: root.get_text_value(self.text)  # pass text value

那么get_text_value()方法可以是:

class MainScreen(Screen):
    def get_text_value(self, text):
        print('text:', text)

【讨论】:

  • 有效!非常感谢您的帮助!非常感谢您帮助我们:)
猜你喜欢
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2017-12-15
  • 2016-03-21
  • 1970-01-01
相关资源
最近更新 更多