1

Bit of a lengthy question. I am creating an entry field (with major help from @skrx) that displays itself on a custom-made screen. I have programmed it so that, when I press the Enter key, the screen should close itself (at the moment this pygame.display.quit thanks to @Sven). But I keep getting an error. My Code:

class InputBox:
def __init__(self, x, y, w, h, text=''):
    self.rect = pygame.Rect(x, y, w, h)
    self.color = COLOR_INACTIVE
    self.text = text
    self.txt_surface = FONT.render(text, True, self.color)
    self.active = False
def handle_event(self, event):
    done = False
    if event.type == pygame.MOUSEBUTTONDOWN: # If the user clicked on the input_box rect.
        if self.rect.collidepoint(event.pos): # Toggle the active variable.
            self.active = not self.active
        else:
            self.active = False # Change the current color of the input box.
        self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
    if event.type == pygame.KEYDOWN:
        if self.active:
            if event.key == pygame.K_BACKSPACE:
                self.text = self.text[:-1]
            elif event.key == pygame.K_RETURN:
                done = True
                pygame.display.quit()
            else:
                self.text += event.unicode # Re-render the text.
            if done == False:
                self.txt_surface = FONT.render(self.text, True, self.color)
            else:
                self.text = "NONE"
                pygame.quit()
def update(self): # Resize the box if the text is too long.
    width = max(300, self.txt_surface.get_width()+10)
    self.rect.w = width
def draw(self, screen):
    screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5)) # Blit the text.
    pygame.draw.rect(screen, self.color, self.rect, 2)# Blit the rect.
def ReturnText(self):
    result = self.text
    return result
#.............
InputBox1 = InputBox(285, 195, 140, 32)
TextMenu = CurrentMenu
done = False
while done != True:
    if done == True:
        break
    else:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.KEYDOWN:
                if InputBox1.active == True:
                    if event.key == pygame.K_RETURN:
                        EventSaveName = InputBox1.ReturnText()
                        pygame.display.quit()
                        done = True
            InputBox1.handle_event(event)  
        InputBox1.update()
        Screen.blit(TextMenu, (0, 0))
        InputBox1.draw(Screen)
        pygame.display.flip()
        clock.tick(30)

I keep getting an error that says:

Screen.blit(TextMenu, (0, 0)) pygame.error: display Surface quit

I have tried a lot of solutions and reading around but I have been unable to find a solution. Therefore I decided to just post my code here and ask straight.

Does anyone know how I can fix the error?

Cheers

JayRoy
  • 105
  • 6

0 Answers0