【问题标题】:Python: TypeError: 'unicode' object is not callablePython:TypeError:'unicode'对象不可调用
【发布时间】:2018-03-06 23:17:16
【问题描述】:

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

这是我的 .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'

当用户输入已存在的用户名时,向用户显示的错误消息如下:

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

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

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

【问题讨论】:

  • 当您将 unicode 对象作为函数调用时会发生此错误。不带括号试试app.new_patient_username
  • @RaghavPatnecha 当我不带括号调用它时,它什么也没做。
  • 我明白,但你这样做的方式也是错误的,因为new_patient_username() 不是可调用函数
  • 我在踢自己。我不小心给这个函数赋予了与对象 new_patient_username 相同的名称。由于它们具有相同的名称,Python 调用的是对象而不是 def 函数。当我更改函数的名称时,它工作正常。非常感谢,因为如果您没有指出它正在调用该对象,我会错过的。
  • 很高兴看到你自己发现了。

标签: python mysql kivy


【解决方案1】:

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

【讨论】:

  • 哈,谢谢,我也做了同样的事情。你的帖子为我节省了很多工作:)
猜你喜欢
  • 2013-03-30
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 2019-03-02
  • 2015-04-26
  • 2020-10-28
相关资源
最近更新 更多