May-26-2019, 05:53 PM
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.
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!
