Вопрос в поле ввода - закрытие окна с продолжением работы Python - PullRequest
1 голос
/ 18 марта 2020

Немного о длинном вопросе. Я создаю поле ввода (с помощью @skrx), которое отображается на экране, сделанном на заказ. Я запрограммировал его так, чтобы при нажатии клавиши Enter экран закрывался сам (на данный момент это pygame.display.quit благодаря @Sven). Но я продолжаю получать ошибку. Мой код:

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)

Я получаю сообщение об ошибке:

Screen.blit (TextMenu, (0, 0)) pygame.error: отображение поверхности quit

Я пробовал много решений и читал вокруг, но я не смог найти решение. Поэтому я решил просто опубликовать свой код здесь и спросить прямо.

Кто-нибудь знает, как я могу исправить ошибку?

Cheers

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...