首页 > 解决方案 > Kivy:AttributeError:'str'对象没有属性'text'

问题描述

'''我对 kivy 比较陌生,所以我真的很感激任何帮助。我一直遇到这个问题:如果 self.name.text != "" and self.username.text != "" and self.password.text != "": AttributeError: 'str' object has no attribute 'text . 在 python 代码的 SignupScreen 中面临这个问题。我尝试将“文本”参数放入:def submit(self, text) 但是,它返回给我另一个错误:TypeError:submit() 缺少 1 个必需的位置参数:'text'。将不胜感激任何建议/帮助。'''

class SignupScreen(Screen):
    name = ObjectProperty(None)
    username = ObjectProperty(None)
    password = ObjectProperty(None)

    def submit(self):
        if self.name.text != "" and self.username.text != "" and self.password.text != "": 
            added = db.add_user(self.username.text, self.password.text, self.name.text)
            if added == 1:
                self.reset()
                wm.current = "login"
            elif added == 2:
                PassTooShort()
            else:
                AccAlreadyExists()
        else:
            invalidForm()

     def login(self):
        self.reset()
        wm.current = "login"

    def reset(self):
        self.username.text = ""
        self.password.text = ""
        self.name.text = ""


# Invalid input handling functions
def invalidLogin():
    pop = Popup(title='Invalid Login',
              content=Label(text='Invalid username\nor password.'),
              size_hint=(None, None), size=(400, 400))
    pop.open()

def invalidForm():
    pop = Popup(title='Invalid Form',
              content=Label(text='Invalid fields. Try again.'),
              size_hint=(None, None), size=(400, 400))

    pop.open()

def AccAlreadyExists():
    pop = Popup(title='Invalid Form (User Exists)',
              content=Label(text='Enter a unique\nUsername.'),
              size_hint=(None, None), size=(400, 400))

    pop.open()

def PassTooShort():
    pop = Popup(title='Invalid Form (Password Too Short)', content=Label(text='Password is too short. Try again.'), size_hint=(None, None), size=(400, 400))

    pop.open()


wm = WindowManager()
db = DataBase("users.db")

wm.current = "welcome"

class MyMainApp(MDApp):
    def build(self):
        self.kv = Builder.load_file("main.kv")
        return self.kv

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

'''下面是我的 Kivy 代码:'''

<SignupScreen>:
    name: "signup"
    n: name
    username: username
    password: passw

    MDFloatLayout:
        md_bg_color: 1,1,1,1
        MDIconButton:
            icon: "arrow-left"
            pos_hint: {"center_y": .95}
            user_font_size: "30sp"
            theme_text_color: "Custom"
            text_color: rgba(26, 24, 58, 255)
            on_release:
                app.root.transition.direction = "right"
                app.root.current = "welcome"    
    
        MDLabel: 
            text: "H i !"
            font_name: "BPoppins"
            font_size: "30sp"
            pos_hint: {"center_x": .5, "center_y": .85}
            halign: "center"
            color: rgba(0, 0, 59, 255)

        MDLabel:
            text: "Create a new account"
            font_name: "MPoppins"
            font_size: "18sp"
            pos_hint: {"center_x": .5, "center_y": .79}
            halign: "center"
            color: rgba(135, 135, 193, 255)

        MDFloatLayout:   #Creating name
            size_hint: .45, .07
            pos_hint: {"center_x": .5, "center_y": .68}
            TextInput:
                id: name
                hint_text: "Name"
                font_name: "MPoppins"
                pos_hint: {"center_x": .445, "center_y": .5}
                background_color: 1,1,1,0          
                foreground_color: rgba(0,0,59,255)
                cursor_color: rgba(0,0,59,255)
                font_size: "14sp"
                cursor_width: "2sp"
                multiline: False
            MDFloatLayout:
                pos_hint: {"center_x": .45, "center_y": 0}
                size_hint_y: .03
                md_bg_color: rgba(178, 178, 178, 255)
    

        MDFloatLayout:   #Creating username
            size_hint: .45, .07
            pos_hint: {"center_x": .5, "center_y": .56}
            TextInput:
                id: username  
                hint_text: "Username"
                font_name: "MPoppins"
                pos_hint: {"center_x": .445, "center_y": .5}
                background_color: 1,1,1,0
                foreground_color: rgba(0,0,59,255)
                cursor_color: rgba(0,0,59,255)
                font_size: "14sp"
                cursor_width: "2sp"
                multiline: False
            MDFloatLayout:
                pos_hint: {"center_x": .45, "center_y": 0}
                size_hint_y: .03
                md_bg_color: rgba(178, 178, 178, 255)
    

        MDFloatLayout:   #Creating password
            size_hint: .45, .07
            pos_hint: {"center_x": .5, "center_y": .44}
            TextInput:
                id: passw
                hint_text: "Password"
                font_name: "MPoppins"
                pos_hint: {"center_x": .445, "center_y": .5}
                background_color: 1,1,1,0
                foreground_color: rgba(0,0,59,255)
                cursor_color: rgba(0,0,59,255)
                font_size: "14sp"
                cursor_width: "2sp"
                multiline: False
                password: True
            MDFloatLayout:
                pos_hint: {"center_x": .45, "center_y": 0}
                size_hint_y: .03
                md_bg_color: rgba(178, 178, 178, 255)

        Button:   #Creating log in button
            text: "SIGN UP"
            size_hint: .35, .065
            pos_hint: {"center_x": .5, "center_y": .3}
            background_color: 0,0,0,0
            font_name: "BPoppins"
            canvas.before:
                Color:
                    rgb: rgba(52, 0, 231, 255)
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [5]
            on_release:            
                app.root.transition.direction = "right"
                root.submit()
                     

        MDLabel:
            text: "or"
            color: rgba(52, 0, 231, 255)
            pos_hint: {"center_y": 0.2}
            font_size: "13sp"
            font_name: "BPoppins"
            halign: "center"
        MDFloatLayout:   
            md_bg_color: rgba(178, 178, 178, 255)
            size_hint: .162, .002
            pos_hint: {"center_x": .405, "center_y": .196}
        MDFloatLayout:   
            md_bg_color: rgba(178, 178, 178, 255)
            size_hint: .162, .002
            pos_hint: {"center_x": .595, "center_y": .196}

        MDTextButton:
            text: "Already have an account?"
            pos_hint: {"center_x": 0.47, "center_y": 0.15}
            color: rgba(68, 78, 132, 255)
            font_size: "12sp"
            font_name: "BPoppins"
    
        MDTextButton:
            text: "Sign in"
            font_name: "BPoppins"
            font_size: "12sp"
            pos_hint: {"center_x": 0.58, "center_y": 0.15}
            color: rgba(52, 0, 213, 255)
            on_release:
                app.root.transition.direction = "left"
                app.root.current = "login"   
        

标签: pythonkivy

解决方案


您的问题的根源是混淆了 的name属性SignupScreen。在更仔细地检查您的代码后,我看到:

    MDFloatLayout:   #Creating name
        size_hint: .45, .07
        pos_hint: {"center_x": .5, "center_y": .68}
        TextInput:
            id: name

和:

<SignupScreen>:
    name: "signup"
    n: name
    username: username
    password: passw

AnyScreen有一个name属性来标识Screen它的ScreenManager. 我相信您的代码将此name属性与用户应输入的名称混淆TextInputidname我建议您使用不同的idObjectProperty保持这些属性清晰。例如,您将代码的第一位冷更改为:

    MDFloatLayout:   #Creating name
        size_hint: .45, .07
        pos_hint: {"center_x": .5, "center_y": .68}
        TextInput:
            id: personname  # different id to clear up confusion

并在SignupScreen规则中:

<SignupScreen>:
    name: "signup"
    personname: personname
    username: username
    password: passw

当然,这需要对SignupScreen代码进行相应的更改:

class SignupScreen(Screen):
    personname = ObjectProperty(None)
    username = ObjectProperty(None)
    password = ObjectProperty(None)

    def submit(self):
        if self.personname.text != "" and self.username.text != "" and self.password.text != "":
            added = db.add_user(self.username.text, self.password.text, self.personname.text)
            if added == 1:
                self.reset()
                wm.current = "login"
            elif added == 2:
                PassTooShort()
            else:
                AccAlreadyExists()
        else:
            invalidForm()

    def login(self):
        self.reset()
        wm.current = "login"

    def reset(self):
        self.username.text = ""
        self.password.text = ""
        self.personname.text = ""

推荐阅读