import pygame import sys pygame.init() display = pygame.display.set_mode((400, 300)) class TextInput(pygame.sprite.Sprite): def __init__(self, x, y, width=100, height=50, color=(0, 0, 0), bgcolor=(100, 255, 100), selectedColor=(0, 0, 255)): super().__init__() self.text_value = "" self.isSelected = False self.color = color self.bgcolor = bgcolor self.selectedColor = selectedColor self.font = pygame.font.SysFont("Verdana", 20) self.text = self.font.render(self.text_value, True, self.color) self.bg = pygame.Rect(x, y, width, height) def clicked(self, mousePos): if self.bg.collidepoint(mousePos): self.isSelected = not self.isSelected return True return False def update_text(self, next_text): temp = self.font.render(next_text, True, self.color) if temp.get_rect().width > (self.bg.width - 20): return self.text_value = next_text self.text = temp def render(self, display): self.pos = self.text.get_rect(center=(self.bg.x + self.bg.width / 2, self.bg.y + self.bg.height / 2)) pygame.draw.rect(display, self.selectedColor if self.isSelected else self.bgcolor, self.bg) display.blit(self.text, self.pos) class CustomGroup(pygame.sprite.Group): def __init__(self): super().__init__() self.current = None TextInputGroup = CustomGroup() TextInputGroup.add(TextInput(x=100, y=100, width=200)) TextInputGroup.add(TextInput(x=100, y=200, width=200)) ibeam = pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_IBEAM) while True: mouse_pos = pygame.mouse.get_pos() clicked_any = False for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: for textinput in TextInputGroup: if textinput.clicked(mouse_pos): if TextInputGroup.current: TextInputGroup.current.isSelected = False TextInputGroup.current = textinput textinput.isSelected = True clicked_any = True break if not clicked_any and TextInputGroup.current: TextInputGroup.current.isSelected = False TextInputGroup.current = None if event.type == pygame.KEYDOWN and TextInputGroup.current: if event.key == pygame.K_BACKSPACE: TextInputGroup.current.update_text(TextInputGroup.current.text_value[:-1]) elif event.key == pygame.K_RETURN: print(TextInputGroup.current.text_value) if event.type == pygame.TEXTINPUT and TextInputGroup.current: TextInputGroup.current.update_text(TextInputGroup.current.text_value + event.text) display.fill((255, 255, 255)) for textinput in TextInputGroup: textinput.render(display) over_textbox = any(textinput.bg.collidepoint(mouse_pos) for textinput in TextInputGroup) pygame.mouse.set_cursor(ibeam if over_textbox else pygame.cursors.Cursor()) pygame.display.update()