I'm trying to create a little text module for pygame. I've got everything up and running but I can't seem to get list.pop() to work. Whenever a person presses a key I put the chr() version of it into a list, then I iterate over that list to print out the word (not the best way, I know, I'm working on it though). Now, whenever I try to put in a backspace nothing happens.

When I try it now it just puts a space into the text. Any help would be great!

Here's part of my code:

def ptext(x, y, text, size=30, color=(255,255,255)):
    font = pygame.font.Font("secrcode.ttf", size)
    img_text = font.render(text, True, color)
    screen = pygame.display.get_surface()
    screen.blit(img_text, (x,y))

def main():
    #different windows
    main_menu = False
    text_box = True
    check_text = False

    pygame.init()
    screen = pygame.display.set_mode((500,500))
    pygame.display.set_caption("Pygame Text-box Prototype")
    font = pygame.font.Font("secrcode.ttf", 30)

    #create the text box
    text = Text_Box(screen)
    word = []

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.type == K_BACKSPACE:
                    word.pop()
                if event.type == K_SPACE:
                    word.append(" ")
                else:
                    word.append(chr(event.key))
        keys = pygame.key.get_pressed()
        if keys[K_ESCAPE]:
            sys.exit()

        start = 90
        try:
            for i in word:
                ptext(start, 260, i)
                start += 17
        except:
            ptext(70, 250, "?")

        ptext(10, 10, "Text Box")
        text.draw_text_box()

        pygame.display.update()

main()

Recommended Answers

All 6 Replies

I can not run the code, after adding import and commenting out fonts I do not have, it fails with not existing TextBox.

if event.type == K_BACKSPACE:
does not work properly, replace with
if chr(event.key) == '\x08':

Also blank out the old text before you rewrite it.

I've tried what you suggested, vegaseat, but I"m still getting a space instead of a backspace. Also, how could I blank out old text?

Thanks.

What version of PyGame are you using?
I tried the changes and they work with PyGame version 1.9.2

You can black out the old screen with screen.fill((0,0,0))

Hi lardmeister.

I'm using 1.9.2 as well.

This is the code I have:

def main():
    #different windows
    main_menu = False
    text_box = True
    check_text = False

    pygame.init()
    screen = pygame.display.set_mode((500,500))
    pygame.display.set_caption("Pygame Text-box Prototype")
    font = pygame.font.Font("secrcode.ttf", 30)

    #create the text box
    text = Text_Box(screen)
    word = []

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.type == K_SPACE:
                    word.append(" ")
                if chr(event.key) == "/x08":
                    word.pop()
                else:
                    word.append(chr(event.key))
        keys = pygame.key.get_pressed()
        if keys[K_ESCAPE]:
            sys.exit()

        start = 90
        pygame.display.update()
        try:
            for i in word:
                ptext(start, 260, i)
                start += 17
        except:
            ptext(70, 250, "?")

        ptext(10, 10, "Text Box")
        text.draw_text_box()

        pygame.display.update()

Here is typical example:

''' pg_texting1.py
display characters in Pygame as they are typed
allows backspacing
'''

import sys
import pygame as pg
from pygame.locals import *  # QUIT, KEYDOWN  etc.

def draw_text(x, y, text, size=30, color=(255, 255, 255)):
    '''display text string at x, y location'''
    font = pg.font.Font(pg.font.get_default_font(), size)
    img_text = font.render(text, True, color)
    screen = pg.display.get_surface()
    screen.blit(img_text, (x,y))


pg.init()
screen = pg.display.set_mode((500, 500))
pg.display.set_caption("Pygame Texting")

#txtcolor = 255, 255, 255  # white
txtcolor = 255, 255, 0  # yellow
bgcolor = 0, 0, 0  # black

clist = []  # char list
# exit on escape key or on window x click
while True:

    keys = pg.key.get_pressed()
    if keys[K_ESCAPE]:
        pygame.quit()
        sys.exit()

    for event in pg.event.get():
        if event.type == QUIT:
            pg.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if chr(event.key) == '\x08':  # backspace
            #if event.type == K_BACKSPACE:  # does not work
                if clist:
                    clist.pop()
            else:
                clist.append(chr(event.key))

            #print(clist)  # testing

    # erase old text
    screen.fill(bgcolor)
    # create a string from the char list
    text = "".join(clist)
    x = 70
    y = 200
    size = 32
    draw_text(x, y, text, size=size, color=txtcolor)

    pg.display.update()
    #pg.display.flip()

I am using Windows7

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.