Python文本输入不是渲染

发布于 2025-01-27 14:53:36 字数 4550 浏览 2 评论 0原文

我正在研究一个项目,我想实现用户的文本输入。它在起作用,但是很难阅读,而且不是很实用,所以我对其进行了重新设计。但是它停止了工作。我查找了互联网,发现此 video 和this stack Overflow 文章,但无法解决问题。怎么了?

全班 +运行方法(我没有错误)

更新:我缺少一行。现在是our错误

import pygame, sys

pygame.init()

class textBox:
    def __init__(self, font, surface, x, y, width, height):
        self.width = width
        self.height = height
        self.inactive = 'blue'
        self.active ='red'
        self.font = font
        self.userText = ''
        self.displaySurface = surface
        self.rect = pygame.Rect(x, y, self.width, self.height)
        self.color = self.inactive
        self.active = False

    def inputCheck(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect.collidepoint(event.pos):
                self.active = not self.active
                pygame.time.wait(100)
            else:
                self.active = False
                pygame.time.wait(100)
            self.color = self.active if self.active else self.inactive
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_BACKSPACE:
                    self.userText = self.userText[:-1]
                if event.key == pygame.K_RETURN:
                    return self.userText
                else:
                    self.userText += event.unicode

    def update(self):
        if len(self.userText) >= 21:
                self.userText = self.userText[:-1]
                print('you cannot type anymore')
        self.rect.w = max(230, self.textSurf.get_width() + 10)

    def renderText(self):
        pygame.draw.rect(self.displaySurface, self.color, self.rect, 4)
        self.textSurf = self.font.render(self.userText, True, (0, 0, 0))
        self.displaySurface.blit(self.textSurf, (self.rect.x + 5, self.rect.y + 5))


bFont = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
box = textBox(bFont, screen, 100, 100, 230, 50)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        

    screen.fill((255, 255, 255))
    box.inputCheck(event)
    box.renderText()
    pygame.display.flip()
    clock.tick(60)

更新:bellow代码是旧的工作代码

import pygame, sys

pygame.init()

class textBox:
    def __init__(self, font, surface, x, y, width, height):
        self.width = width
        self.height = height
        self.inactive = 'blue'
        self.active ='red'
        self.font = font
        self.userText = ''
        self.displaySurface = surface
        self.rect = pygame.Rect(x, y, self.width, self.height)
        self.color = self.inactive
        self.active = False

    def inputCheck(self):
        self.userText += event.unicode

    def renderText(self):
        pygame.draw.rect(self.displaySurface, self.color, self.rect, 4)
        textSurf = self.font.render(self.userText, True, (0, 0, 0))
        self.displaySurface.blit(textSurf, (self.rect.x + 5, self.rect.y + 5))
        if len(self.userText) >= 21:
                self.userText = self.userText[:-1]
                print('you cannot type anymore')
        self.rect.w = max(230, textSurf.get_width() + 10)


bFont = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
box = textBox(bFont, screen, 100, 100, 230, 50)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if box.rect.collidepoint(event.pos):
                box.active = not box.active
                pygame.time.wait(100)
            else:
                box.active = False
                pygame.time.wait(100)
            box.color = box.active if box.active else box.inactive
        if event.type == pygame.KEYDOWN:
            if box.active:
                if event.key == pygame.K_BACKSPACE:
                    box.userText = box.userText[:-1]
                if event.key == pygame.K_RETURN:
                    pass
                else:
                    box.inputCheck()

    screen.fill((255, 255, 255))
    box.renderText()
    pygame.display.flip()
    clock.tick(60)

I'm working on a project and I want to implement user's text input. It was working, but was too hard to read and was not very practical, so I reworked it. But it stopped working. I looked up the internet and found this video and this stack overflow article, but couldn't resolve the problem. What is wrong?

Whole class + run method (I'm not having an error)

Update: I had one line missing. Now it's withour an error

import pygame, sys

pygame.init()

class textBox:
    def __init__(self, font, surface, x, y, width, height):
        self.width = width
        self.height = height
        self.inactive = 'blue'
        self.active ='red'
        self.font = font
        self.userText = ''
        self.displaySurface = surface
        self.rect = pygame.Rect(x, y, self.width, self.height)
        self.color = self.inactive
        self.active = False

    def inputCheck(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect.collidepoint(event.pos):
                self.active = not self.active
                pygame.time.wait(100)
            else:
                self.active = False
                pygame.time.wait(100)
            self.color = self.active if self.active else self.inactive
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_BACKSPACE:
                    self.userText = self.userText[:-1]
                if event.key == pygame.K_RETURN:
                    return self.userText
                else:
                    self.userText += event.unicode

    def update(self):
        if len(self.userText) >= 21:
                self.userText = self.userText[:-1]
                print('you cannot type anymore')
        self.rect.w = max(230, self.textSurf.get_width() + 10)

    def renderText(self):
        pygame.draw.rect(self.displaySurface, self.color, self.rect, 4)
        self.textSurf = self.font.render(self.userText, True, (0, 0, 0))
        self.displaySurface.blit(self.textSurf, (self.rect.x + 5, self.rect.y + 5))


bFont = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
box = textBox(bFont, screen, 100, 100, 230, 50)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        

    screen.fill((255, 255, 255))
    box.inputCheck(event)
    box.renderText()
    pygame.display.flip()
    clock.tick(60)

Update: The code bellow is the old working code

import pygame, sys

pygame.init()

class textBox:
    def __init__(self, font, surface, x, y, width, height):
        self.width = width
        self.height = height
        self.inactive = 'blue'
        self.active ='red'
        self.font = font
        self.userText = ''
        self.displaySurface = surface
        self.rect = pygame.Rect(x, y, self.width, self.height)
        self.color = self.inactive
        self.active = False

    def inputCheck(self):
        self.userText += event.unicode

    def renderText(self):
        pygame.draw.rect(self.displaySurface, self.color, self.rect, 4)
        textSurf = self.font.render(self.userText, True, (0, 0, 0))
        self.displaySurface.blit(textSurf, (self.rect.x + 5, self.rect.y + 5))
        if len(self.userText) >= 21:
                self.userText = self.userText[:-1]
                print('you cannot type anymore')
        self.rect.w = max(230, textSurf.get_width() + 10)


bFont = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
box = textBox(bFont, screen, 100, 100, 230, 50)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if box.rect.collidepoint(event.pos):
                box.active = not box.active
                pygame.time.wait(100)
            else:
                box.active = False
                pygame.time.wait(100)
            box.color = box.active if box.active else box.inactive
        if event.type == pygame.KEYDOWN:
            if box.active:
                if event.key == pygame.K_BACKSPACE:
                    box.userText = box.userText[:-1]
                if event.key == pygame.K_RETURN:
                    pass
                else:
                    box.inputCheck()

    screen.fill((255, 255, 255))
    box.renderText()
    pygame.display.flip()
    clock.tick(60)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕烟庭风 2025-02-03 14:53:36

问题在于box.inputcheck(event)在事件循环后发射。
事件循环可以一次性地持有多个事件,例如textInputkeydown。当InputCheck调用时,它将最后一个事件作为参数传递。

解决方案是将box.inputcheck移动到事件循环中:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    box.inputCheck(event)

The problem is that box.inputCheck(event) is firing after the event loop.
The event loop can hold multiple events at a single time like TextInput and KeyDown. When inputCheck calls it passes the last event as the argument.

A solution would be to move box.inputCheck into the event loop:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    box.inputCheck(event)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文