1

I've just started out coding in python, and I was playing around with text boxes, when I got an error (AttributeError: 'Event' object has no attribute 'unicode'). From what I've found on a few forums, this error is because I use Python 3, but I couldn't find anywhere an alternative I could use.

for event in pygame.event.get():

        # quit
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False

            if event.type == pygame.KEYDOWN:

                # Check for backspace
                if event.key == pygame.K_BACKSPACE:

                    user_text = user_text[:-1]

            else:
                user_text += event.unicode   # here is where I recieve the error

The exact error is:

 user_text += event.unicode
              ^^^^^^^^^^^^^
AttributeError: 'Event' object has no attribute 'unicode'

I've tried using str, and some other solutions but I didn't manage to implement them.

Kornelius
  • 11
  • 1
  • Does this answer your question? [How can I create a text input box with Pygame?](https://stackoverflow.com/questions/46390231/how-can-i-create-a-text-input-box-with-pygame) – Jared Smith Dec 27 '22 at 20:21

1 Answers1

0

I think you have your indentation levels confused.

If the event is QUIT, you quit.

If the event is MOUSEBUTTONDOWN, you check if it's KEYDOWN, and if it's not, you add the unicode attribute to the value. I don't understand how the event could be both MOUSEBUTTONDOWN and KEYDOWN. I suggest you double-check the layout of those if statements.

Simon Lundberg
  • 1,413
  • 2
  • 11
  • 23