I am creating a Python program in Kivy that allows users to sign in, or create a new account. I have a table of usernames and passwords in MySQL that the Python program refers to (via PyMySQL). When the new user enters a username that already exists, Python iterates through every username in the MySQL table, and if the username exists already, Python asks the user to enter a new password. The issue is that when the user attempts to enter a new username and Python runs through the code again I get the "TypeError: 'unicode' object is not callable". I am puzzled because if the user enters a username that doesn't already exist the first time, I get no errors. When they attempt to enter the username again, I get the error.
Here is my .py main file code block that is giving me problems:
#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
Here is my .kv code:
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'
Here is the error message displayed to the user when they enter a username that already exists:
When they enter a new username, this is the Traceback that I get:
on_release: app.new_patient_username()
TypeError: 'unicode' object is not callable
As you can see, when the user presses the "store data" button a second time, a tracebook occurs. However, when they press the Button the first time and attempt to store a username and password, no error occurs.
app.new_patient_username
– Raghav Patnecha Mar 6 '18 at 15:09new_patient_username()
is not a callable function – Raghav Patnecha Mar 6 '18 at 15:17