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

时间:2018-03-06 15:01:21

标签: python mysql kivy

我正在Kivy中创建一个允许用户登录或创建新帐户的Python程序。我有一个Python程序引用的MySQL用户名和密码表(通过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'

以下是用户输入已存在的用户名时显示的错误消息:enter image description here

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

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

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

1 个答案:

答案 0 :(得分:2)

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