You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从KivyMD文本框获取值?点击添加按钮崩溃的解决方法

KivyMD应用点击按钮崩溃、无法获取文本框值的问题分析与修复

问题原因

  1. 类方法参数错误:doThis方法未使用self作为第一个参数,导致无法在方法内访问应用实例的核心属性(比如根部件root)。
  2. ID访问逻辑错误:Python代码中直接使用root.ids无效,root是KV语言专属关键字,Python环境下需要通过应用实例的self.root访问根部件,再获取其下属的ID控件。

修复方案

修改Test类中的doThis方法,同时修正文本框值的获取逻辑,以下是完整修复后的代码:

from kivy.lang import Builder
from kivymd.app import MDApp

class Test(MDApp):

    def doThis(self):
        # 通过self.root访问根部件,获取各文本框的值
        first_feet = self.root.ids.oft.text
        first_inches = self.root.ids.oin.text
        second_feet = self.root.ids.tft.text
        second_inches = self.root.ids.tin.text
        
        # 打印验证获取到的值
        print(f"1st Length: {first_feet}' {first_inches}\"")
        print(f"2nd Length: {second_feet}' {second_inches}\"")
        
        # 可在此添加英尺英寸相加的计算逻辑,示例:
        # total_inches = int(first_feet)*12 + int(first_inches) + int(second_feet)*12 + int(second_inches)
        # result_feet = total_inches // 12
        # result_inches = total_inches % 12
        # 将结果赋值给界面标签
        # self.root.ids.result_feet.text = str(result_feet)
        # self.root.ids.result_inches.text = str(result_inches)

    def build(self):
        self.theme_cls.material_style = "M3"
        self.theme_cls.primary_palette = "Red"
        self.theme_cls.theme_style = "Light"
        return Builder.load_string(
'''
MDScreen:

    MDBottomNavigation:
        panel_color: "#FFCCD5"
        selected_color_background: "#FF758F"
        text_color_active: "black"

        MDBottomNavigationItem:
            name: 'home'
            text: 'Home'
            icon: 'home'
            md_bg_color: "#FFF0F3"

            MDAnchorLayout:
            
                anchor_x:'center'
                anchor_y:'top'
            
                MDBoxLayout:
                    orientation: 'vertical'
                    adaptive_height: True
                    
                    MDTopAppBar:
                        title: "Feet Adder"
                        elevation: 0
                        anchor_title: "left"
                        md_bg_color: "#FFF0F3"
                        specific_text_color:"black"
                          
                    MDLabel:
                        text: 'Enter the values'
                        text_size: root.width, root.height
                        padding: 0, 50, 0, 0
                        markup: True
                        halign: 'center'
                        valign: 'top'
                        
                    MDBoxLayout:
                        orientation: 'horizontal'
                        adaptive_height: True
                        padding: 20,50,20,20
                        spacing: dp(10)    
                        
                        MDLabel:
                            text: "1st Length"
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                                      
                        MDTextField:
                            hint_text: "Feet"
                            mode: "rectangle"
                            id :oft
                            
                        MDTextField:
                            hint_text: "Inches"
                            mode: "rectangle"
                            id :oin
                            
                    MDBoxLayout:
                        orientation: 'horizontal'
                        adaptive_height: True
                        padding: 20,10,20,10
                        spacing: dp(10)
                                  
                        MDLabel:
                            text: "2nd Length"
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                            
                        MDTextField:
                            hint_text: "Feet"
                            mode: "rectangle"
                            id: tft
                            
                        MDTextField:
                            hint_text: "Inches"
                            mode: "rectangle"
                            id : tin
                            
                    MDAnchorLayout:
                        anchor_x:'center'
                        padding: 0,80,0,0
                              
                        MDFillRoundFlatButton:
                            text: "Add"
                            md_bg_color: "#A4133C"
                            halign: 'center'
                            on_release: app.doThis()
                            
                    MDBoxLayout:
                        orientation: 'horizontal'
                        adaptive_height: True
                        padding: 20,100,20,20
                        spacing: dp(10)
                        MDLabel:
                            text:"Result:- "
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                        MDLabel:
                            text:""
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                            id: result_feet
                        MDLabel:
                            text:'"'
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                        MDLabel:
                            text:""
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                            id: result_inches
                        MDLabel:
                            text:"'"
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None

        MDBottomNavigationItem:
            name: 'about'
            text: 'Info'
            icon: 'information'
            badge_icon: "numeric-1"

            MDLabel:
                text: 'InfoINRO'
                halign: 'center'

'''
        )

Test().run()

额外优化点:

  • 修正了原代码中"2st Length"的拼写错误,改为"2nd Length"
  • 给结果展示标签添加了ID,方便后续将计算结果直接更新到界面

内容的提问来源于stack exchange,提问作者Ninja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 11:04:52