【问题标题】:Problem with referring to a class variable in kv language and in python code在 kv 语言和 python 代码中引用类变量的问题
【发布时间】:2022-01-17 16:26:39
【问题描述】:

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

这里是代码。

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() 被执行时,它会打印“Email: None Password: None”。我按照 youtube 上的教程进行操作,但我不明白出了什么问题。提前谢谢你。

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    在您的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
    

    【讨论】:

    • 谢谢你真棒
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多