I'm making a program and I'd like to run it using pygame. The key thing is that I need an space for the user to input numbers and at the screen I need to print an array, which may vary in size (always 3 columns, but the user controls the number of lines), but I want it to always been fully shown in the screen. How can I make both things?
-
2SO is definitely not the place for you to ask for people to write you some code. Look up tutorials on pygame and learn the module. Write your code and if you run into problems come here. Don't rely on people to do what you need to do :) – Daniel May 02 '16 at 21:47
-
Use a GUI-toolkit. On most platforms Python comes with the `tkinter` toolkit. – Roland Smith May 02 '16 at 23:00
3 Answers
You can't expect people to write down code from scratch to accomplish what you want. If it is just advice you need, I can give some.
Pygame has no formal input field or input box introduced. If you want to implement such thing in pygame specifically, you have to keep record of the input keys(keyboard input). And insert the inputs in a string or list, then display them yourself. But you have to implement the methodology of the inputbox you designed, like you should have a condition for backspace where it deletes the last element of the list. Pygame does not do this formally, but you can write something like this.
However, what you need seems like a gui. Which gives a better way of taking inputs, making input fields, giving properties to the program window and the input boxes, making buttons etc.. In this case, if you change your mind, I would suggest Wxpython.

- 2,938
- 1
- 13
- 38
If you want to create a very simple text input box on your own, you could try to define an area with a pygame.Rect, then check if the mouse collides with the rect to select it and use the .unicode
attribute of the keyboard events to attach the characters to a list or string which you can display on the screen.
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
your_input_string += event.unicode
There are also some GUI libraries for Pygame like SGC which is pretty easy to use. Alternatives (on pygame.org) are Albow, PGU and OcempGUI (the latter works only with Python 2.7).

- 19,980
- 5
- 34
- 48
This can be done using pygame_gui. Text input is available through a UITextEntryLine
instance, while printing an array could be done using a UITextBox
. You'll first need to set up the environment as in the quick start guide.
Create a UITextEntryLine
instance:
from pygame.rect import Rect
from pygame_gui.elements.ui_text_entry_line import UITextEntryLine
text_input = UITextEntryLine(relative_rect=Rect(0, 0, 100, 100), manager=manager)
Input can be restricted to only numbers using set_allowed_characters
:
text_input.set_allowed_characters('numbers')
Adding a check to the event queue allows for getting text input when enter is pressed:
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
if event.user_type == pygame_gui.UI_TEXT_ENTRY_FINISHED:
if event.ui_element == text_input:
entered_text = text
A UITextBox
can be set to a custom height by setting the height to -1:
from pygame_gui.elements.ui_text_box import UITextBox
text_box = UITextBox(relative_rect=Rect(0, 0, 100, -1), manager=manager, text='foobar')

- 325
- 4
- 9
-
1Please try to provide information necessary to solve problem without need of links, your other answer looks fine but if by some coincidence gets removed one day, this answer will be almost unusable. – Ruli Nov 18 '20 at 07:26
-
ah, okay. I can see how that would be an issue. It will be fixed shortly. – Umbral Reaper Nov 18 '20 at 07:32