Nov-03-2018, 12:55 PM
Hey!
I'm doing my A-Level computing project and have come across a problem. My program has a login system in which there is a database with usernames/passwords and entering your username/password into a textbox will then check to see if that is in the database and let you log in if it is. My issue is that I can't find a way to return information from the textbox function to be able to use it in my database search. Can anyone help, please?
Here is my textbox code.
I'm doing my A-Level computing project and have come across a problem. My program has a login system in which there is a database with usernames/passwords and entering your username/password into a textbox will then check to see if that is in the database and let you log in if it is. My issue is that I can't find a way to return information from the textbox function to be able to use it in my database search. Can anyone help, please?
Here is my textbox code.
def textbox(): # creating the textbox
inputBox = pygame.Rect(50, 185, 500, 80)
inactiveColour = BLUE
activeColour = LIGHTBLUE
colour = inactiveColour
active = False
done = False
text = ' '
while not done:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN: # change textbox colour when clicked
if inputBox.collidepoint(event.pos):
active = not active # changes from whatever the state currently is
else:
active = False
if active:
colour = activeColour
else:
colour = inactiveColour
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN: # information will be returned when enter key is pressed
print(text)
text = ' '
done = True
elif event.key == pygame.K_BACKSPACE: # a letter will be removed if backspace is pressed
text = text[:-1]
else:
text = text + event.unicode # otherwise, text will be added to the textbox
text_surface = buttonText.render(text, True, colour)
width = max(500, text_surface.get_width() + 10) # width of textbox increases if there is not enough space
inputBox.w = width
screen.blit(text_surface, (inputBox.x+5, inputBox.y+5)) # blit textbox onto screen
pygame.draw.rect(screen, colour, inputBox, 2)
pygame.display.flip() # update game display
return textHere is the code where I am attempting to retrieve the information put into the textbox.def loginScreen():
screen.fill(WHITE)
usernameSurface = lettersTitle.render('Please enter your username:', False, BLACK)
screen.blit(usernameSurface, (50, 50))
username = textbox()
print("username is " + username)The username prints in the textbox function, but when the last print statement in the loginScreen function is reached, it simply prints "username is " with nothing afterwards.
