无法访问类外的类变量 - kivy

时间:2017-03-31 12:54:43

标签: kivy

任何时候我尝试运行我的应用程序时都会收到错误。

错误:AttributeError:'NoneType'对象没有属性'text'

请参阅下面的代码段

class Login(Screen):

    email_obj = ObjectProperty()
    password_obj = ObjectProperty()
    conn = lite.connect("database/nairamanager.db")
    cursor = conn.cursor()

    def init(self, **kwargs):
        super().__init__(**kwargs)

    # Display an alert on login validation
    def login_alert(self):
        p = LoginAlert()
        p.open()

    # Get user from database
    def login_nm_user(self):
        self.cursor.execute("SELECT * FROM user_tb WHERE email_db = ? AND password_db = ?;", (self.email_obj.text, self.password_obj.text))
        if self.cursor.fetchall():
            self.manager.current = "news_feed_screen"
        else:
            return self.login_alert()
            self.conn.commit()
            # self.conn.close

    # Get user email
    def get_user_email(self):
        self.cursor.execute("SELECT * FROM user_tb WHERE email_db = ?;", (self.email_obj.text))
        self.user_email = self.cursor.fetchone()[0]
        self.conn.commit()
        print(self.user_email)


login = Login()
login.get_user_email()
print(login.user_email)

.kv代码 下面是kivy lang文件中的Login类

<Login>:

email_obj: email_text
password_obj: password_text


GridLayout:
    size: root.size
    rows: 2
    rows_minimum: {0:0,1:root.height*.7}
    spacing: 50
    GridLayout:
        rows: 2
        row_default_minimum: True
        rows_minimum: {0:root.height*.1,1:root.height*.01}
        BoxLayout:
            orientation: "horizontal"
            BackButton:
                text: "L"
                size_hint: (.1,1)
                halign: "center"
                markup: True
                on_press:
                    root.manager.current = "login_signup_screen"
                    root.manager.transition.direction = "left"
            TopLabel:
                text: "[b]Lets get you logged in...[/b]"
                size_hint: (.9,1)
        BorderLabel:

    # Login form
    BoxLayout:
        orientation: "vertical"
        spacing: 20
        UniTextInput:
            id: email_text
            hint_text: "Email"
            size_hint: (.6,1)
            pos_hint: {"center_x":.5}
        UniTextInput:
            id: password_text
            hint_text: "Password"
            password: True
            size_hint: (.6,1)
            pos_hint: {"center_x":.5}
        GreenButton
            id: main_login_button
            text: "Login"
            size_hint: (.6,1)
            pos_hint: {"center_x":.5}
            on_press:
                root.login_nm_user()
                root.get_user_email()

        # Fake widget for spacing
        Widget:
        Widget:
        Widget:

拜托,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

要将变量共享到所有屏幕,您可以将其设置为screenmanager的属性 像这样:

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from kivy.properties import StringProperty


class MyScreenManager(ScreenManager):
    user_email = StringProperty()

class Login(Screen):

    def login(self, user, pw):
        # login
        # if pass
        self.manager.user_email = user
        self.manager.current = "screen2"


root = Builder.load_string('''

MyScreenManager:

    Login:
        name: "login"
        id: login
        BoxLayout:

            orientation: 'vertical'

            TextInput:
                id: email_input
            TextInput:
                id: password_input
            Button:
                text: "Login"
                on_release: login.login(email_input.text, password_input.text)


    Screen:
        name: "screen2"
        BoxLayout:
            orientation: "vertical"    
            Label:
                text: "Wellcome {}!".format(root.user_email)
            Button:
                text: "Go to screen 3" 
                on_release: root.current = "screen3"

    Screen:
        name: "screen3"
        BoxLayout:
            orientation: "vertical"    
            Label:
                text: "Wellcome {}!".format(root.user_email)
            Button:
                text: "Go back to screen 2" 
                on_release: root.current = "screen2"

''')

class MyApp(App):

    def build(self):
       return root

MyApp().run()

那就是说,我看到你定义了一个init方法。如果你真的想覆盖__init__,请记住下划线 在这个特定的例子中,你真的没有在init中做任何事情。当您创建新的super().__init__()

时,Screen会被执行