在 kv 语言和 python 代码中引用 class 变量的问题

Problem with referring to a class variable in kv language and in python code

您好,我很头疼地想了解为什么如果我尝试将信息插入我的登录页面并将其打印到控制台中,它不起作用。谁能帮帮我?

这是代码。

main.py

from kivy.app import App
from kivy.config import Config

Config.set('graphics','width',360)
Config.set('graphics','height',640)
Config.set('graphics', 'resizable', False)

from App.constructor import Constructor


class MyApp(App):
    def build(self):
        return Constructor().constr()


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

constructor.py

from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen


class LoginPage(Screen):
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def btn(self):
        print('Email: ', self.email.text, 'Password: ', self.password.text)


class SignUpPage(Screen):
    email = ObjectProperty(None)
    password = ObjectProperty(None)
    prof = ObjectProperty(None)


class Home(Screen):
    pass
    # def __init__(self):
    #     if prof == True:
    #         self.btn = Button(text='Gestionale per i Clienti',
    #                           on_release='app.root.current = home')
    #         self.blank = Label(text='')
    #         Widget.add_widget(self.btn)
    #         Widget.add_widget(self.blank)


class WindowManager(ScreenManager):
    pass


class Constructor():
    def constr(self):
        return Builder.load_file('constructor.kv')

constructor.kv

<LoginPage>:
    name: "login"

    BoxLayout:

        email: email
        password: password

        orientation: "vertical"
        pos_hint: {"center_x": .5}
        size_hint: .7, 1

        Label:
            text: "Email:"

        TextInput:
            id: email
            multiline: False

        Label:
            text: "Password:"

        TextInput:
            id: password
            multiline: False
            # password: True

        Label:
            text: ""

        Button:
            text: "Submit"
            on_release:
                # app.root.current = "home"
                # root.manager.transition.direction = "left"
                root.btn()

        Label:
            text: ""

        Button:
            text_align: "center"
            text: "Non hai un account?\nCreane uno qui!"
            on_release:
                app.root.current = "signup"
                root.manager.transition.direction = "right"

        Label:
            text: ""

当函数 btn() 被执行时,它打印“电子邮件:None 密码:None”。我遵循了 youtube 上的教程,但我不明白哪里出了问题。提前谢谢你。

在您的 kv 文件中,您将 emailpassword 定义为 BoxLayout 的属性:

BoxLayout:

    email: email
    password: password

尝试将这些属性向上移动到 LoginPage:

<LoginPage>:
    name: "login"
    email: email
    password: password

    BoxLayout:

        orientation: "vertical"
        pos_hint: {"center_x": .5}
        size_hint: .7, 1