0

我正在尝试在控制台中打印登录凭据,但是如果该函数没有显示错误,则凭据也没有打印。我想将 check_data_login 函数绑定到 kv 文件中定义的“拍摄”按钮,该函数已定义在 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'



我该如何解决这个问题

4

1 回答 1

1

.py文件中的问题很少.kv。在kv文件中idFieldRound文本字段不应该有引号。并且在按钮丢失的on_release情况下也是如此。所以更正后的文件应该如下:shootapp.check_data_login()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'

更正后的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):
        username = self.root.ids.username.text
        password = self.root.ids.password.text

        print(username)
        print(password)


DemoApp().run()
于 2020-08-14T17:15:02.067 回答