I want a text box to appear on the screen. I am doing this by:

TextButton= pygame.draw.rect(Screen,Red, (100, 100, 150,50),2)

How can I allow the user to enter a text into this text box in Pygame? The text box should say 'Enter your name here'. When the user clicks on it, they should be able to enter their name, and submit it if the textbox is not empty. How can I do this in pygame?

share|improve this question

Pygame does not support this feature. Usually this sort of thing is handled by GUI libraries, but I'm not aware of any that works with pygame, so I think you have to build it yourself. Here's what you need to do:

  • Keep a string for user input
  • Draw that string, and whatever else you need for the text box
  • When the user has pressed a key:
    • If it's a character, add it to the string
    • If it's a special key like backspace, delete the last character

That gives you the bare minimum, if you want other features like movable cursor, you need to write more code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.