Python Forum
[Pygame] Problems with my textbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pygame] Problems with my textbox
#1
I was trying to make a text box, but I ran into a few problems. Here are the problems:
Problem 1 - Backspace will delete the first letter it is looking fro from the list. Meaning if you type "How are you", backspace will delete the "o" in how.

Problem 2 - Caps lock will lock caps, but will not unlock caps

The file here is the file I use for buttons, message_display, etc. The TextBox class is on line 56. Caps lock is on line 106 and backspace is on line 109. If testing, use the start function to execute the Text Box.
import pygame
from functools import partial
import time

class Screen_Display():
    def text_objects(text, font, color):
        textSurface = font.render(text, True, color)
        return textSurface, textSurface.get_rect()

    def message_display(gD, text, size, color, centerX, centerY):
        font = pygame.font.SysFont('arial', size)
        textSurf, TextRect = Screen_Display.text_objects(text, font, color)
        TextRect.center = ((centerX),(centerY))
        gD.blit(textSurf, TextRect)

class Button():
    def __init__(self, rect, text, textsize, textcolor):
        self.rect = rect
        self.font = pygame.font.SysFont('arial', textsize)
        self.textSurf, self.textRect = Screen_Display.text_objects(text, self.font, textcolor)
        self.textRect.center = ((rect[0] + (rect[2]/2), rect[1] + (rect[3]/2)))

    def draw(self, gD, ButColor):
        pygame.draw.rect(gD, ButColor, (self.rect))
        gD.blit(self.textSurf, self.textRect)

    def optClick(self, gD, ShadowColor, ButColor, command=None, command2=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
            pygame.draw.rect(gD, ShadowColor, (self.rect))
            gD.blit(self.textSurf, self.textRect)
            if click[0] == 1:
                    if command != None:
                        command()
                    if command2 != None:
                        command2()
            else:
                pygame.draw.rect(gD, ButColor, (self.rect))
                gD.blit(self.textSurf, self.textRect)

class InvisButton():
    def __init__(self, rect):
        self.rect = rect

    def optClick(self, command=None, command2=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
            if click[0] == 1:
                if command != None:
                    command()
                if command2 != None:
                    command2()

class TextBox():
    def __init__(self, rect, placeholdertext, textsize, textcolor):
        self.rect = rect
        self.font = pygame.font.SysFont('arial', textsize)
        self.textSurf, self.textRect = Screen_Display.text_objects(placeholdertext, self.font, textcolor)
        self.textRect.center = ((rect[0] + (rect[2]/2), rect[1] + (rect[3]/2)))
        self.placeholdertext = placeholdertext
        self.textcolor = textcolor
        self.text = ''

    def draw(self, Screen, ButtonColor):
        pygame.draw.rect(Screen, ButtonColor, self.rect)
        Screen.blit(self.textSurf, self.textRect)

    def optType(self, Screen, ButtonColor, command):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
            Screen.blit(self.textSurf, self.textRect)
            if click[0] == 1:
                command()

    def Type(self, Screen, ButtonColor, maxCharacters):
        typeList = []
        keyDict = {pygame.K_q : 'q', pygame.K_w : 'w', pygame.K_e : 'e', pygame.K_r : 'r',
                   pygame.K_t : 't', pygame.K_y : 'y', pygame.K_u : 'u', pygame.K_i : 'i',
                   pygame.K_o : 'o', pygame.K_p : 'p', pygame.K_a : 'a', pygame.K_s : 's',
                   pygame.K_d : 'd', pygame.K_f : 'f', pygame.K_g : 'g', pygame.K_h : 'h',
                   pygame.K_j : 'j', pygame.K_k : 'k', pygame.K_l : 'l', pygame.K_z : 'z',
                   pygame.K_x : 'x', pygame.K_c : 'c', pygame.K_v : 'v', pygame.K_b : 'b',
                   pygame.K_n : 'n', pygame.K_m : 'm', pygame.K_1 : '1', pygame.K_2 : '2',
                   pygame.K_3 : '3', pygame.K_4 : '4', pygame.K_5 : '5', pygame.K_6 : '6',
                   pygame.K_7 : '7', pygame.K_8 : '8', pygame.K_9 : '9', pygame.K_0 : '0',
                   pygame.K_SPACE : ' ', pygame.K_TAB : '   '
                   }
        caps = False
        capBool = False
        while True:
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
            typeStr = ''
            count = 0

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
                        if caps:
                            caps = False
                        else:
                            caps = True
                    elif event.key == pygame.K_BACKSPACE:
                        if len(typeList) != 0:
                            typeList.remove(typeList[len(typeList) - 1])
                    elif event.key == pygame.K_CAPSLOCK:
                        if not capBool:
                            caps = True
                            capBool = True
                        else:
                            caps = False
                            capBool = False
                    elif event.key == pygame.K_RETURN or mouse[0] < self.rect[0] or  mouse[0] > self.rect[0] + self.rect[2] or mouse[1] < self.rect[1] or mouse[1] > self.rect[1] + self.rect[3]:
                        if event.key == pygame.K_RETURN:
                            break
                        elif click[0] == 1:
                            break
                    elif event.key in keyDict:
                        if caps and len(typeList) < maxCharacters:
                            typeList.append(keyDict[event.key].upper())
                        elif len(typeList) < maxCharacters:
                            typeList.append(keyDict[event.key])
                        
                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
                        if caps == False:
                            caps = True
                        else:
                            caps = False
                        

            #Print
            for character in typeList:
                typeStr += character
            self.text = typeStr
            typeStr += '|'
            textSurf, textRect = Screen_Display.text_objects(typeStr, self.font, self.textcolor)
            pygame.draw.rect(Screen, ButtonColor, self.rect)
            Screen.blit(textSurf, self.textRect)
            pygame.display.update()

    def start(self, Screen, ButtonColor, maxCharacters):
        self.draw(Screen, ButtonColor)
        self.optType(Screen, ButtonColor, partial(self.Type, Screen, ButtonColor, maxCharacters))
Thanks in advance!
Reply
#2
input boxes can get quite complicated actually. Especially in pygame where you have to build it from scratch. I would suggest to use existing ones or use existing ones to apply to your own. Sorry dont have time to look at yours now.

You can take a look at these for inspiration :
https://github.com/metulburr/pygooey
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame installation problems Gheryk 5 8,497 Nov-29-2023, 08:49 PM
Last Post: E_Mohamed
  [PyGame] Problems with jump code in pygame Joningstone 4 5,271 Aug-23-2021, 08:23 PM
Last Post: deanhystad
Photo Textbox is being overdrawn G_rizzle 2 2,543 Apr-15-2021, 04:43 PM
Last Post: metulburr
  [pygame] Inventory problems. Weapons equipped to wrong slot SheeppOSU 6 3,942 May-07-2019, 02:46 AM
Last Post: SheeppOSU
  drawing, moving, and collision problems (pygame) SheeppOSU 26 14,436 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU
  Problems with loading buttons (pygame) SheeppOSU 2 3,054 Apr-12-2019, 08:04 PM
Last Post: SheeppOSU
  [PyGame] Returning information from textbox? mzmingle 5 3,074 Nov-03-2018, 09:34 PM
Last Post: mzmingle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020