Python:TypeError:“unicode”对象不可调用

Sim*_*abo 0 python mysql kivy

我正在 Kivy 中创建一个 Python 程序,它允许用户登录或创建一个新帐户。我在 MySQL 中有一个 Python 程序引用的用户名和密码表(通过 PyMySQL)。当新用户输入一个已经存在的用户名时,Python 会遍历 MySQL 表中的每个用户名,如果用户名已经存在,Python 会要求用户输入一个新密码。问题是,当用户尝试输入新用户名并且 Python 再次运行代码时,我收到“TypeError: 'unicode' object is not callable”。我很困惑,因为如果用户第一次输入一个不存在的用户名,我不会出错。当他们再次尝试输入用户名时,我收到错误消息。

这是我的 .py 主文件代码块,它给我带来了问题:

#Select all usernames from the database with the execute method
cur.execute("""SELECT username FROM patient_login""")

#Use the fetchall() method to get a list dictionary of all usernames
self.usernames_from_db = cur.fetchall()


#For each of the names in the list/dict of usernames
for names in self.usernames_from_db:

      #If the names username is equal to the username the user created
      if names['username'] == self.new_patient_username:

            #Display an error if username exists
            self.root.ids.new_username_error_box.text = 'Username taken'

            #Empty the username TextInput box
            self.root.ids.new_patient_username.text = ''

            #Empty the password TextInput box
            self.root.ids.new_patient_password.text = ''




        #Else store the username
        else:
            cur.execute("""INSERT INTO patient_login (username, 
                         password) VALUES ("%s", "%s")"""
                         %s(self.new_patient_username, 
                            self.new_patient_password))

            #Commit to database
            user_db_conn.commit()#Commit it to the database







            #Select the ID for the username with the execute method 
            cur.execute("""SELECT ID FROM patient_login WHERE username="%s"
                        """%(self.new_patient_username))


            #Use the fetchall() method to get the ID stored into a list dict
            self.user_id_dict = cur.fetchall()

            #Get the 0 (first) index in the dictionary of ID's.
            self.user_id_index = self.user_id_dict[0]

            #Get the 'ID' VALUE in the key value pair of the ID dict. Make 
            self.user_id_string = str(self.user_id_index['ID'])

            #Get the integer value of the user id as well
            self.user_id_integer = int(self.user_id_string)

            #Display the ID in a TextInput Box so that the user knows their
            self.root.ids.new_username_error_box.text = self.user_id_string
Run Code Online (Sandbox Code Playgroud)

这是我的 .kv 代码:

  Screen:

    name: 'Create_Username'

    GridLayout:
        rows: 4
        columns: 2
        spacing: 2
        padding: 2

        Label: 
            text: 'Username'

        TextInput:
            text:''
            id: new_patient_username


        Label:
            text:'Password'

        TextInput:
            text:''
            id: new_patient_password

        TextInput:
            text:''
            id:new_username_error_box

        Button:
            text: 'Enter Data'
            on_release: app.new_patient_username()
            background_normal: 'red_button_background.jpg'

        Button:
            text:'Continue'
            on_release: root.current = 'Create_Account'
            background_normal: 'red_button_background.jpg'

        Button:
            text: 'Back'
            on_release: root.current = 'Sign_In_To_Account'
            background_normal: 'red_button_background.jpg'
Run Code Online (Sandbox Code Playgroud)

这是当用户输入已存在的用户名时向用户显示的错误消息: 在此处输入图片说明

当他们输入新用户名时,这是我得到的回溯:

     on_release: app.new_patient_username()
     TypeError: 'unicode' object is not callable
Run Code Online (Sandbox Code Playgroud)

如您所见,当用户第二次按下“存储数据”按钮时,会出现跟踪簿。但是,当他们第一次按下按钮并尝试存储用户名和密码时,不会发生错误。

Sim*_*abo 7

踢我自己。我的函数名称与函数内的变量名称相匹配。当我调用函数时,它正在调用变量(不能调用字符串/Unicode 对象),而不是调用函数。我只是更改了函数的名称,代码运行良好。