Text input box in pygame/python

2020-05-03 11:20发布

I am working on an RPG in pygame/python. I have made a char. creator that allows you to customize the player. Now I am looking for a way to prompt for a name on-screen. I don't want it to make a box, just print what the user is typing in a specific area (see picture). Thanks for help.

http://ubuntuone.com/3HdzOKroopUEf1YxqNnbFM <-----Picture (only looks blue through the link)

3条回答
乱世女痞
2楼-- · 2020-05-03 11:44

You can capture the events and if event.type == KEYDOWN, then check for event.key to get which key is the user pressing. Then you can add it to a text variable and show it on the screen.

查看更多
萌系小妹纸
3楼-- · 2020-05-03 11:53

You could also use EzText. It is a module that basically does what FRJA describes for you. There are plenty of other modules if you google "pygame text input." Here is EzText's example code:

# EzText example
from pygame.locals import *
import pygame, sys, eztext

def main():
    # initialize pygame
    pygame.init()
    # create the screen
    screen = pygame.display.set_mode((640,240))
    # fill the screen w/ white
    screen.fill((255,255,255))
    # here is the magic: making the text input
    # create an input with a max length of 45,
    # and a red color and a prompt saying 'type here: '
    txtbx = eztext.Input(maxlength=45, color=(255,0,0), prompt='type here: ')
    # create the pygame clock
    clock = pygame.time.Clock()
    # main loop!

    while 1:
        # make sure the program is running at 30 fps
        clock.tick(30)

        # events for txtbx
        events = pygame.event.get()
        # process other events
        for event in events:
            # close it x button si pressed
            if event.type == QUIT: return

        # clear the screen
        screen.fill((255,255,255))
        # update txtbx
        txtbx.update(events)
        # blit txtbx on the sceen
        txtbx.draw(screen)
        # refresh the display
        pygame.display.flip()

if __name__ == '__main__': main()
查看更多
Rolldiameter
4楼-- · 2020-05-03 12:02

I have recently written another module that makes it even easier to insert text. You simply create a TextInput-object, then feed it with events every frame of your game and finally retreive the rendered surface using get_surface().

Here's an example program that demonstrates how to use it:

import pygame_textinput # Import the textinput-module
import pygame
pygame.init()

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

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.get_surface(), (10, 10))

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

If you want to process the user input after he pressed return, simply wait until the update()-method returns True:

if textinput.update(events):
    foo()

More detailed information and the source code can be found on [my github page](my github page.

查看更多
登录 后发表回答