KivyMD中KV语言绑定按钮与get_texts函数报错求助
解决KivyMD函数绑定的AttributeError问题
错误原因
报错'FloatLayout' object has no attribute 'get_texts'是因为kv文件中root指代的是最外层的FloatLayout实例,而get_texts方法定义在你的App(MDApp子类)中,FloatLayout自然没有这个方法,导致调用失败。
解决方法
1. 用app调用App类的方法
在kv文件中,app是MDApp实例的内置引用,直接用它来调用get_texts即可:
修改按钮的on_release属性:
MDRaisedButton: # 其余属性保持不变 on_release: app.get_texts()
2. 修正代码拼写错误
你的main.py里存在一处拼写错误:self.root.ids.passoword中的passoword应该改为password(和kv文件里的输入框id一致),否则会找不到对应的文本框。修正后的代码片段:
def get_texts(self): user_text_field = self.root.ids.username user_text = user_text_field.text password_text_field = self.root.ids.password # 修正拼写错误 password_text = password_text_field.text quantity_text_field = self.root.ids.quantity quantity_text = quantity_text_field.text return user_text, password_text, quantity_text
完整修改后的代码
main.py
from kivymd.tools.hotreload.app import MDApp from kivy.lang import Builder from kivy.core.window import Window class App(MDApp): def get_texts(self): user_text_field = self.root.ids.username user_text = user_text_field.text password_text_field = self.root.ids.password password_text = password_text_field.text quantity_text_field = self.root.ids.quantity quantity_text = quantity_text_field.text # 可添加打印测试,确认内容获取正常 print(f"用户名: {user_text}, 密码: {password_text}, 数量: {quantity_text}") return user_text, password_text, quantity_text KV_FILES = ["app.kv"] DEBUG = True def build_app(self): Window.size = (320, 370) Window.resizable = 0 return Builder.load_file("app.kv") App().run()
app.kv
FloatLayout: #:kivy ! Image: source: "src/images/instagram_bg.jpg" allow_stretch: True keep_ratio: False MDLabel: text: "Comment Bot" color: "#FFFFFF" font_size: "38sp" font_name: "font.ttf" pos_hint: {"center_x": 0.59, "center_y": 0.9} MDIcon: icon: "instagram" color: "#FFFFFF" font_size: "45sp" pos_hint: {"center_x": 0.85, "center_y": 0.90} MDTextField: id: username line_color_normal: "#ffffff" line_color_focus: "#ffffff" hint_text: "Nome de usuário ou e-mail" size_hint_x: .8 pos_hint: {"center_x": 0.5, "center_y": 0.75} mode: "rectangle" hint_text_color_normal: "white" hint_text_color_focus: "white" text_color_focus: "#ffffff" text_color_normal: "#ffffff" font_size: "20sp" MDTextField: id: password line_color_normal: "#ffffff" line_color_focus: "#ffffff" text_color: "#ffffff" hint_text: "Senha" size_hint_x: .8 pos_hint: {"center_x": 0.5, "center_y": 0.57} mode: "rectangle" hint_text_color_normal: "white" hint_text_color_focus: "white" text_color_focus: "#ffffff" text_color_normal: "#ffffff" font_size: "20sp" MDTextField: id: quantity color_mode: "primary" line_color_normal: "#ffffff" line_color_focus: "#ffffff" hint_text: "Quantidade de comentários" size_hint_x: .8 pos_hint: {"center_x": 0.5, "center_y": 0.39} mode: "rectangle" hint_text_color_normal: "white" hint_text_color_focus: "white" text_color_focus: "#ffffff" text_color_normal: "#ffffff" font_size: "20sp" MDRaisedButton: text: "Iniciar" theme_text_color: "Custom" text_color: "#FFFFFF" font_style: "Button" md_bg_color: "#C8439C" size_hint: .5, .13 pos_hint: {"center_x": .5, "center_y": .155} font_size: "16sp" on_release: app.get_texts()
修改完成后,点击按钮即可正确调用get_texts方法,获取输入框内容。
内容的提问来源于stack exchange,提问作者Kauã Guilherme da Silva Leal
相关产品推荐
相关产品推荐

