Skip to main content

Enter text using pygame

Project description

Pygame Text Input Module

This module provides two utility classes that simplify entering text using pygame. The classes are:

  • TextInputVisualizer which can be used to both manage and draw text input. Simply pass all events returned by pygame.event.get() to it every frame, and blit its surface attribute on the screen.
  • TextInputManager that can be used to just manage inputted text, with no visual aspect. Used by TextInputVisualizer behind the scenes.

Example of module in use

Installation

Simplest way is using pypi:

python3 -m pip install pygame-textinput

Usage

Visualizer

The easiest way is to TextInputVisualizer without any arguments. Then, feed all pygame events to its update method every frame, and blit it's surface property to the screen. Here's a minimal example:

import pygame_textinput
import pygame
pygame.init()

# Create TextInput-object
textinput = pygame_textinput.TextInputVisualizer()

screen = pygame.display.set_mode((1000, 200))
clock = pygame.time.Clock()

while True:
    screen.fill((225, 225, 225))

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exit()

    # Feed it with events every frame
    textinput.update(events)
    # Blit its surface onto the screen
    screen.blit(textinput.surface, (10, 10))

    pygame.display.update()
    clock.tick(30)

Notes on the newer version:

  • You have to watch for "return" presses by the user yourself, e.g. like this:
if [ev for ev in events if ev.type == pygame.KEYDOWN and ev.key == pygame.K_RETURN]:
    print("Oooweee")
  • Contrary to the old version, key-stroke repeats are not manually introduced anymore, since they can now be enabled within pygame directly:
pygame.key.set_repeat(200, 25) # press every 50 ms after waiting 200 ms

This new version has also been optimized such that you can modify any fields on the fly and the actual surface will only re-render if you access it using textinput.surface - and only if you actually modified any values.

Arguments / Fields:

All these values can be both specified as arguments to the constructor and modified at later time by setting them as attributes (e.g. textinput.font_color = (255, 0, 0)). The surface itself will only re-render once it is accessed via textinput.surface.

Argument Description
manager The TextInputManager used to manage the input
font_object The pygame.font.Font object used for rendering
antialias whether to render the font antialiased or not
font_color color of font rendered
cursor_blink_interval The interval of the cursor blinking, in ms
cursor_width The width of the cursor, in pixels
cursor_color The color of the cursor

Manager

If you prefer to draw the text on the screen yourself, you can use TextInputManager to only manage the string that has been typed so far.

Like TextInputVisualizer, you feed its update method all events received by pygame.event.get() which you want it to process. TextInputVisualizer does this for you inside its update method if you pass it a TextInputManager.

Arguments:

Argument Description
initial The initial value (text)
validator A function taking a string and returning a bool. Every time the input value is modified, this function is called; if the function returns True, the input is accepted, otherwise it is ignored.

So say you want to only allow input to up to 5 letters, you could do that with

manager = TextInputManager(validator=lambda input: len(input) <= 5)

Fields

Field Description
value The inserted value so far. When change, cursor_pos is kept as far as possible.
cursor_pos The position of the cursor. 0 is before the first character, len(manager.value) the position after the last. Values outside this range are clamped.

Example

Here's an example that shows most features:

import pygame
import pygame.locals as pl

pygame.init()

# No arguments needed to get started
textinput = TextInputVisualizer()

# But more customization possible: Pass your own font object
font = pygame.font.SysFont("Consolas", 55)
# Create own manager with custom input validator
manager = TextInputManager(validator = lambda input: len(input) <= 5)
# Pass these to constructor
textinput_custom = TextInputVisualizer(manager=manager, font_object=font)
# Other customizations:
textinput_custom.cursor_width = 4
textinput_custom.cursor_blink_interval = 400 # blinking interval in ms
textinput_custom.antialias = False
textinput_custom.font_color = (0, 85, 170)

screen = pygame.display.set_mode((1000, 200))
clock = pygame.time.Clock()

# Pygame now allows natively to enable key repeat:
pygame.key.set_repeat(200, 25)

while True:
    screen.fill((225, 225, 225))

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exit()

    # Feed it with events every frame
    textinput.update(events)
    textinput_custom.update(events)

    # Get its surface to blit onto the screen
    screen.blit(textinput.surface, (10, 10))
    screen.blit(textinput_custom.surface, (10, 50))

    # Modify attributes on the fly - the surface is only rerendered when .surface is accessed & if values changed
    textinput_custom.font_color = [(c+10)%255 for c in textinput_custom.font_color]

    # Check if user pressed return
    if [ev for ev in events if ev.type == pygame.KEYDOWN and ev.key == pygame.K_RETURN]:
        print(f"User pressed enter! Input so far: {textinput.value}")

    pygame.display.update()
    clock.tick(30)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygame-textinput-1.0.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

pygame_textinput-1.0.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file pygame-textinput-1.0.1.tar.gz.

File metadata

  • Download URL: pygame-textinput-1.0.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.9

File hashes

Hashes for pygame-textinput-1.0.1.tar.gz
Algorithm Hash digest
SHA256 31efab5b171c179553ebc226ba3e639802be3b6d7b224f01fe3b6064effe5a5d
MD5 2c17499bef35b0f27bb2f40c3bda9743
BLAKE2b-256 0379cda2841983f40e56fa19edad0443453e2ae9b9d9d6449fbb1bcc13e6b761

See more details on using hashes here.

File details

Details for the file pygame_textinput-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pygame_textinput-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.9

File hashes

Hashes for pygame_textinput-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0aea142473c5651f87cb08a1af41a77137a89688aa4dae2677603da7565cff36
MD5 686ab7de5e402c4f46c4db41e382b4cc
BLAKE2b-256 82aaee16c4eea5ced8ea444aa37bf7106588f0d33a0ef75a8766bfad0b0448af

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page