我正在try 使用KivyMD和Python制作一个基本的用户界面(我是初学者),但我有一些投标方面的问题.如果你们中有人能帮我,我将不胜感激.这是我的IDE返回给我的错误:

文件"C:\USERS\kaua.silva\Desktop\app_ig\app.kv",第67行,位于 ON_RELEASE:root.get_Text() ^^ 文件"kivy\weismproxy.pyx",第32行,在kivy.nedproxy.WeakProxy.getattr中. AttributeError:"FloatLayout"对象没有属性""Get_Text""

main.py file:

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_field = user_text_field.text

        password_text_field = self.root.ids.passoword
        password_text_field = password_text_field.text

        quantity_text_field = self.root.ids.quantity
        quantity_text_field = quantity_text_field.text

        return user_text_field, password_text_field, quantity_text_field

    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 file:

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: root.get_texts()

我想用MDRaisedButton绑定函数GET_TEXTS(它只是一个简单的函数,我将用另一个函数替换它),但vscode无法识别该函数.谢谢你的到来!

推荐答案

try 如下所示的方法代码:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window

class MyApp(MDApp):
    def build(self):
        Window.size = (320, 370)
        Window.resizable = 0
        return Builder.load_file("app.kv")

    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

if __name__ == "__main__":
    MyApp().run()

我已经更改了app.kv,如下所示:

FloatLayout:
    Image:
        source: "src/images/instagram_bg.jpg"
        allow_stretch: True
        keep_ratio: False
    # add your other widgets here . . . (your other widgets)
    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()

请试一下那些密码.

Python相关问答推荐

获取2个字节之间的异或

使用子字符串动态更新Python DataFrame中的列

Pandas使用过滤器映射多列

Django文件上传不起作用:文件未出现在媒体目录或数据库中

自定义新元未更新参数

inspect_asm不给出输出

强制venv在bin而不是收件箱文件夹中创建虚拟环境

Odoo -无法比较使用@api.depends设置计算字段的日期

使用polars .滤镜进行切片速度比pandas .loc慢

分组数据并删除重复数据

Python:在类对象内的字典中更改所有键的索引,而不是仅更改一个键

Pydantic 2.7.0模型接受字符串日期时间或无

运行Python脚本时,用作命令行参数的SON文本

从numpy数组和参数创建收件箱

pandas滚动和窗口中有效观察的最大数量

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

为什么np. exp(1000)给出溢出警告,而np. exp(—100000)没有给出下溢警告?

如何使用使用来自其他列的值的公式更新一个rabrame列?

在Admin中显示从ManyToMany通过模型的筛选结果

导入错误:无法导入名称';操作';