按钮的绑定方法-Kivy

Binding method to button - Kivy

Python代码:

class Test(MDApp):
    def build(self):
        return Builder.load_file('test.kv')
    def btn(self):
        print('Button Pressed')

kv代码:

MDScreen:
    MDRaisedButton:
        text:'Submit'
        on_press: root.btn()

按下按钮时,我收到此错误消息:

AttributeError: 'MDScreen' object has no attribute 'btn'

我做错了什么?

顺便说一句,我正在使用 kivymd,它是 kivy 的一个分支,工作方式几乎相同。

on_press: root.btn()

如错误所述,root 评估为您的 MdScreen 实例,即 kv 规则的根。你想要 app.btn().

BTW, I am using kivymd which is a fork of kivy and works almost the same way.

KivyMD 不是 Kivy 的分支,它是一个依赖于 Kivy 的模块。