Python Forum
[PyGame] Returning information from textbox?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Returning information from textbox?
#1
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.

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 text
Here 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.
Reply


Messages In This Thread
Returning information from textbox? - by mzmingle - Nov-03-2018, 12:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Textbox is being overdrawn G_rizzle 2 2,611 Apr-15-2021, 04:43 PM
Last Post: metulburr
  [Pygame] Problems with my textbox SheeppOSU 1 3,100 May-27-2019, 12:03 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020