简体   繁体   中英

I am not sure why but I am getting error: “AttributeError: 'super' object has no attribute '__getattr__'”

I have some code:

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

Builder.load_file('design.kv')

class AttrGetter:
    def __getattr__(self, item):
        raise AttributeError(item)


class WelcomePage(Screen):
    def go_to_sign_up(self):
        self.manager.current = "register_page"
    def go_to_login(self):
        self.manager.current = "login_screen"

class RegisterPage(Screen, AttrGetter):
    def add_user(self, schoolname, contactname, jobtitle, schoolemailaddress, phonenumber, numberofyeargroupstoberegistered, numberofregisteringchildren, whichyeargroupsbeingregistered):
        with open("register_details.json") as file:
            register_details = json.load(file)

            register_details[schoolname] = {'school_name': schoolname, 'contact_name': contactname, 'job_title': jobtitle,
            'school_email_address': schoolemailaddress, 'phone_number': phonenumber, 'number_of_year_groups_to_be_registered':numberofyeargroupstoberegistered, 'number_of_registering_children': numberofregisteringchildren, 'which_year_groups_being_registered': whichyeargroupsbeingregistered}
            
            with open("register_details.json", "w") as file:
                json.dump(register_details, file)
            self.manager.current = 'register_page_success'
    def go_to_login(self):
        self.manager.current = "login_screen"
    



class LoginScreen(Screen, AttrGetter):
    def login(self, emailID, password):
        with open("register_details.json", encoding = 'utf-8') as file:
            register = json.load(file)
        if emailID.values() == emailID and password.values() == password:#emailID in register and password in register:
            print('hi!')#self.manager.current = 'login_screeen_success'
        else:
            print('bye!')#self.ids.login_wrong.text = "Wrong username or password!"

class RegisterPageSuccess(Screen):
    pass

class RootWidget(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        return RootWidget()


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

Then I also have a kivy file:

<WelcomePage>:
GridLayout:
    cols: 1
    GridLayout:
        cols: 1
        Label:
            text: "Welcome"
        Button:
            text: "register"
            on_press: root.go_to_sign_up
        Button:
            text: "login"
            on_press: root.go_to_login


<RegisterPage>:
    GridLayout:
        cols: 1
        GridLayout:
            cols: 1
            Label:
                text: "Register Page"
                font_size: "20sp"
            TextInput:
                id: schoolname
                hint_text: "School Name"
            TextInput:
                id: contactname
                hint_text: "Contact Name"
            TextInput:
                id: jobtitle
                hint_text: "Job Title"
            TextInput:
                hint_text: "emailID"
            TextInput:
                id: phonenumber
                hint_text: "Phone number"
            TextInput:
                id: numberofyeargroupstoberegistered
                hint_text: "Number of Year group to be registered"
            TextInput:
                id: numberofregisteringchildren
                hint_text: "Nummber of registering children"
            TextInput:
                id: whichyeargroupsbeingregistered
                hint_text: "Which yeargroups being registered"
            TextInput:
                id: password
                hint_text: "Password"
            Button:
                text: "Submit"
                on_press: root.add_user(root.ids.schoolname.text, root.ids.contactname.text, root.ids.jobtitle.text, root.ids.schoolemailaddress.text, root.ids.phonenumber.text, root.ids.numberofyeargroupstoberegistered.text, root.ids.numberofregisteringchildren.text, root.ids.whichyeargroupsbeingregistered.text)
            Button:
                text: "Already have an account? Click here to log in"
                on_press: root.go_to_login()


<LoginScreen>:
    GridLayout:
        cols: 1
        GridLayout:
            cols: 1
            Label:
                text: "Login Page"
                font_size: "20sp"
            TextInput:
                id: emailID
                hint_text: "emailID"
            TextInput:
                id: password
                password: True
                hint_text: "Password"
            Button:
                text: "Login"
                on_press: root.login(emailID, password)
            Button:
                text: "Don't have an account? Click here to sign up"
                on_press: root.go_to_sign_up()

<RegisterPageSuccess>:
    GridLayout:
        cols: 1
        GridLayout:
            cols: 1
            Label: 
                text: "You have logged in!"

<RootWidget>:
    # WelcomePage:
    #     name: "welcome_page"
    RegisterPage:
        name: "register_page"
    LoginScreen:
        name: "login_screen"
    RegisterPageSuccess:
        name: "register_page_success"

For some reason I get the error: "AttributeError: 'super' object has no attribute ' getattr '" This error comes when I put in values for the login page textinput and the registerpage one to. I ahve tried looking for a solution but cannot find one. When I put in values in the register_page part of the code, I need the program to store the data I give it into a certain empty json file called 'register_details.json'. Then, obviously, when I put in the emailID and password into the login_screen, it needs to check if the emailID and password are correct from the json file.

You are trying to access a non-existent id : schoolemailaddress . You can fix that by simply defining it:

        TextInput:
            id: schoolemailaddress
            hint_text: "emailID"

Also, in your kv , you can reference objects directly using their id . You do not need the root.ids prefix:

        Button:
            text: "Submit"
            on_press: root.add_user(schoolname.text, contactname.text, jobtitle.text, schoolemailaddress, phonenumber.text, numberofyeargroupstoberegistered.text, numberofregisteringchildren.text, whichyeargroupsbeingregistered.text)
            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM