Reputation: 33
I've been working in my spare time on a simple game using pygame to allow me to get familiar with its code. The game I am making is a abstraction of a Four in a Row game which implements maths questions. To get the user's answer I am using a module called pygame_textinput. However, I am struggling to extract the user's answer which I want to then be able to compare to the correct answer, which will allow the user to place their disk in the grid.
The code for this I have is:
mathsanswer = pygame_textinput.TextInput()
This is outside of the main loop which I then call upon in the code below.
mathsanswer.update(events) #Check if user has inputted text
display.blit(mathsanswer.get_surface(), (600,725))
This part of the code works perfectly as the text the user types is displayed on screen.
However when I try to extract what the user has typed I get:
<pygame_textinput.TextInput object at 0x00000219C1F101D0>
Is there a way to get what the user has typed as the variable.
Thanks for any help.
Upvotes: 2
Views: 369
Reputation: 2211
Try this.. you need to use get_text()
to get the input.
To catch the user input after the user hits Return, simply evaluate the return value of the update()-method - it is always False except for when the user hits Return, then it's True. To get the inputted text, use get_text(). Example:
if mathsanswer.update(events):
print(mathsanswer.get_text())
Upvotes: 1