Python:TypeError:';unicode';对象不可调用

Python:TypeError:';unicode';对象不可调用,python,mysql,kivy,Python,Mysql,Kivy,我正在Kivy中创建一个Python程序,允许用户登录或创建一个新帐户。我在MySQL中有一个Python程序(通过PyMySQL)引用的用户名和密码表。当新用户输入一个已经存在的用户名时,Python会遍历MySQL表中的每个用户名,如果用户名已经存在,Python会要求用户输入一个新密码。问题是,当用户尝试输入新用户名,Python再次运行代码时,我得到“TypeError:'unicode'对象不可调用”。我很困惑,因为如果用户第一次输入的用户名不存在,我就不会出错。当他们再次尝试输入用户

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

下面是给我带来问题的.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
这是我的.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'
以下是当用户输入已存在的用户名时显示给用户的错误消息:

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

     on_release: app.new_patient_username()
     TypeError: 'unicode' object is not callable

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

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

将unicode对象作为函数调用时会发生此错误。尝试不带括号
应用程序。新患者用户名
@RaghavPatnecha当我不带括号调用它时,它没有任何作用。我理解,但你这样做也是错误的,因为
新患者用户名()
不是一个可调用的函数。我在踢自己。我不小心给了函数与对象new_patient_username相同的名称。由于它们的名称相同,Python调用的是对象而不是def函数。当我更改函数的名称时,它工作得很好。非常感谢,因为如果你没有指出它正在调用对象,我会错过的。很高兴看到你自己发现了。哈,谢谢,我也做了同样的事情。你的帖子帮我省了很多工作:)