【问题标题】:how to bind function to button in kv file如何将函数绑定到kv文件中的按钮
【发布时间】:2020-12-04 11:26:18
【问题描述】:

我正在尝试在控制台中打印登录凭据,但如果该功能没有显示错误,则凭据也没有打印。我想将 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'



我该如何解决这个问题

【问题讨论】:

    标签: python function kivy bind kivymd


    【解决方案1】:

    .py.kv 文件中几乎没有问题。在kv 文件中,idFieldRound 文本字段不应包含引号。在shoot 按钮的on_release 事件中,app.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()
    

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 2020-07-11
      • 1970-01-01
      相关资源
      最近更新 更多