Entry Field Question - Closing the Window but keeping Python Running

醉酒当歌 提交于 2020-04-07 07:11:24

问题


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

来源:https://stackoverflow.com/questions/60742853/entry-field-question-closing-the-window-but-keeping-python-running

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!