Python 如何将函数绑定到kv文件中的按钮

Python 如何将函数绑定到kv文件中的按钮,python,function,kivy,bind,kivymd,Python,Function,Kivy,Bind,Kivymd,我正试图在控制台中打印登录凭据,但如果该函数未显示错误,则凭据也不会打印。我想将check_data_login函数绑定到kv文件中定义的“shoot”按钮上。该函数在py文件中定义。我无法使用ID绑定它。它显示错误,表示该对象不存在。 这是我的.py文件 from kivymd.app import MDApp from kivy.uix.boxlayout import BoxLayout from kivy.properties import ObjectProperty clas

我正试图在控制台中打印登录凭据,但如果该函数未显示错误,则凭据也不会打印。我想将check_data_login函数绑定到kv文件中定义的“shoot”按钮上。该函数在py文件中定义。我无法使用ID绑定它。它显示错误,表示该对象不存在。 这是我的.py文件

from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty




class DemoApp(MDApp):
    def check_data_login(self,obj):
        username = self.username.text
        password = self.password.text

        print(username)
        print(password)


DemoApp().run()
这是我的.kv文件

<WhiteLabel@MDLabel>
    size_hint_y:None
    height:self.texture_size[1]
    theme_text_color:"Custom"


<FieldRound@MDTextFieldRound>
    pos_hint:{"center_x":0.5}
    normal_color:0,1,1,0.5
    icon_left_color:1,1,1,1

ScreenManager:
    id: sm
    Screen:
        id:s1
        name:"s1"
        manager:sm
        FitImage:
            source:"bg4.jpg"
        MDToolbar:
            md_bg_color:1,0,156/255,1
            title:"My Demo App"
            pos_hint:{"center_x":0.5,"center_y":0.95}
            elevation:11

        BoxLayout:
            orientation:"vertical"
            size_hint_y:None
            height:self.minimum_height
            pos_hint:{"center_x":0.5,"center_y":0.5}
            padding:"24dp","24dp","24dp","24dp"
            spacing:"12dp"
            size_hint_x:0.85


            canvas:
                Color:
                    rgba: 1,1,1,1
                RoundedRectangle:
                    pos: self.pos
                    size: self.size
                    radius: [10,10,10,10]



            WhiteLabel:
                text:"Welcome to My GUI!!"
                text_color:1,0,156/255,1
                bold:True
                halign:"center"
                font_style:"Caption"
                font_size:25
            Widget:
                size_hint_y:None
                height:"12dp"

            WhiteLabel:
                text:"We just need your details."
                font_style:"H1"
                font_size:20

            WhiteLabel:
                text:"Please enter carefully"
                font_style:"H1"
                font_size:20
            Widget:
                size_hint_y:None
                height:"12dp"
            FieldRound:
                id:'username'
                icon_left:"mail"
                hint_text:"Username"
                normal_color:1,1,1,1
                color_active:1,1,1,1
            FieldRound:
                id:'password'
                icon_left:"key"
                hint_text:"Password"
                normal_color:1,1,1,1
                color_active:1,1,1,1
            Widget:
                size_hint_y:None
                height:"12dp"
            MDFillRoundFlatButton:
                id:'bu'
                text:"Shoot"
                text_color:1,0,156/255,1
                md_bg_color:247/255,171/255,1,1
                pos_hint:{"center_x":0.5}
                on_release:
                    app.check_data_login
                    sm.current = 's2'
        MDFlatButton:
            elevation_normal: 12
            text:"Signup"
            text_color:1,1,1,1
            md_bg_color:1,0,156/255,1
            pos_hint:{"center_x":0.3,"center_y":0.1}
            on_release:
                sm.current = 's2'
        MDFlatButton:
            elevation_normal: 12
            text:"Login"
            text_color:1,1,1,1
            md_bg_color:1,0,156/255,1
            pos_hint:{"center_x":0.7,"center_y":0.1}
            on_release:
                sm.current = 's2'




    Screen:
        id:s2
        name:"s2"
        manager:sm
        BoxLayout:
            Image:
                source:"rajat.jpg"
                opacity:0.5

        MDRaisedButton:
            text:"button"
            pos_hint:{"center_x":0.5,"center_y":0.2}
            on_press:
                sm.current ='s1'




尺寸提示:无
高度:自身纹理大小[1]
主题\文本\颜色:“自定义”
pos_提示:{“center_x”:0.5}
正常颜色:0,1,1,0.5
图标\u左\u颜色:1,1,1,1
屏幕管理器:
id:sm
屏幕:
id:s1
名称:“s1”
经理:sm
FitImage:
资料来源:“bg4.jpg”
MDToolbar:
md_bg_颜色:1,0156/255,1
标题:“我的演示应用程序”
pos_提示:{“center_x”:0.5,“center_y”:0.95}
立面图:11
盒子布局:
方向:“垂直”
尺寸提示:无
高度:自身最小高度
pos_提示:{“center_x”:0.5,“center_y”:0.5}
填充:“24dp”、“24dp”、“24dp”、“24dp”
间距:“12dp”
尺寸:0.85
画布:
颜色:
rgba:1,1,1,1
圆反射角:
pos:self.pos
大小:self.size
半径:[10,10,10,10]
白标:
文字:“欢迎使用我的GUI!!”
文字颜色:1,0156/255,1
黑体字:对
哈利恩:“中心”
字体样式:“标题”
字体大小:25
小装置:
尺寸提示:无
高度:“12dp”
白标:
文本:“我们只需要你的详细信息。”
字体样式:“H1”
字体大小:20
白标:
文字:“请仔细输入”
字体样式:“H1”
字体大小:20
小装置:
尺寸提示:无
高度:“12dp”
实地调查:
id:“用户名”
图标左:“邮件”
提示文字:“用户名”
正常颜色:1,1,1,1
活动颜色:1,1,1,1
实地调查:
id:“密码”
图标左:“键”
提示文字:“密码”
正常颜色:1,1,1,1
活动颜色:1,1,1,1
小装置:
尺寸提示:无
高度:“12dp”
MDFillRoundFlatButton:
id:‘bu’
文字:“射击”
文字颜色:1,0156/255,1
md_bg_颜色:247/255171/255,1,1
pos_提示:{“center_x”:0.5}
发布时:
app.check\u数据\u登录
sm.current='s2'
MDFlatButton:
正常高度:12
正文:“注册”
文本颜色:1,1,1,1
md_bg_颜色:1,0156/255,1
pos_提示:{“center_x”:0.3,“center_y”:0.1}
发布时:
sm.current='s2'
MDFlatButton:
正常高度:12
文本:“登录”
文本颜色:1,1,1,1
md_bg_颜色:1,0156/255,1
pos_提示:{“center_x”:0.7,“center_y”:0.1}
发布时:
sm.current='s2'
屏幕:
编号:s2
名称:“s2”
经理:sm
盒子布局:
图片:
资料来源:“rajat.jpg”
不透明度:0.5
MDRaisedButton:
文本:“按钮”
pos_提示:{“center_x”:0.5,“center_y”:0.2}
新闻界:
sm.current='s1'

如何解决此问题在
.py
.kv
文件中几乎没有问题。在
kv
文件中,
FieldRound
文本字段的
id
不应带有引号。也可以在
on\u release
事件中的
shot
按钮中选择
app。检查
登录时的
数据是否缺少
()
。因此,更正的
kv
文件应如下所示:

<WhiteLabel@MDLabel>
    size_hint_y:None
    height:self.texture_size[1]
    theme_text_color:"Custom"


<FieldRound@MDTextFieldRound>
    pos_hint:{"center_x":0.5}
    normal_color:0,1,1,0.5
    icon_left_color:1,1,1,1

ScreenManager:
    id: sm
    Screen:
        id:s1
        name:"s1"
        manager:sm
        FitImage:
            source:"bg4.jpg"
        MDToolbar:
            md_bg_color:1,0,156/255,1
            title:"My Demo App"
            pos_hint:{"center_x":0.5,"center_y":0.95}
            elevation:11

        BoxLayout:
            orientation:"vertical"
            size_hint_y:None
            height:self.minimum_height
            pos_hint:{"center_x":0.5,"center_y":0.5}
            padding:"24dp","24dp","24dp","24dp"
            spacing:"12dp"
            size_hint_x:0.85


            canvas:
                Color:
                    rgba: 1,1,1,1
                RoundedRectangle:
                    pos: self.pos
                    size: self.size
                    radius: [10,10,10,10]



            WhiteLabel:
                text:"Welcome to My GUI!!"
                text_color:1,0,156/255,1
                bold:True
                halign:"center"
                font_style:"Caption"
                font_size:25
            Widget:
                size_hint_y:None
                height:"12dp"

            WhiteLabel:
                text:"We just need your details."
                font_style:"H1"
                font_size:20

            WhiteLabel:
                text:"Please enter carefully"
                font_style:"H1"
                font_size:20
            Widget:
                size_hint_y:None
                height:"12dp"
            FieldRound:
                id: username
                icon_left:"mail"
                hint_text:"Username"
                normal_color:1,1,1,1
                color_active:1,1,1,1
            FieldRound:
                id: password
                icon_left:"key"
                hint_text:"Password"
                normal_color:1,1,1,1
                color_active:1,1,1,1
            Widget:
                size_hint_y:None
                height:"12dp"
            MDFillRoundFlatButton:
                id:'bu'
                text:"Shoot"
                text_color:1,0,156/255,1
                md_bg_color:247/255,171/255,1,1
                pos_hint:{"center_x":0.5}
                on_release:
                    app.check_data_login()
                    sm.current = 's2'
        MDFlatButton:
            elevation_normal: 12
            text:"Signup"
            text_color:1,1,1,1
            md_bg_color:1,0,156/255,1
            pos_hint:{"center_x":0.3,"center_y":0.1}
            on_release:
                sm.current = 's2'
        MDFlatButton:
            elevation_normal: 12
            text:"Login"
            text_color:1,1,1,1
            md_bg_color:1,0,156/255,1
            pos_hint:{"center_x":0.7,"center_y":0.1}
            on_release:
                sm.current = 's2'




    Screen:
        id:s2
        name:"s2"
        manager:sm
        BoxLayout:
            Image:
                source:"rajat.jpg"
                opacity:0.5

        MDRaisedButton:
            text:"button"
            pos_hint:{"center_x":0.5,"center_y":0.2}
            on_press:
                sm.current ='s1'
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty




class DemoApp(MDApp):
    def check_data_login(self):
        username = self.root.ids.username.text
        password = self.root.ids.password.text

        print(username)
        print(password)


DemoApp().run()