Python文本输入不是渲染
我正在研究一个项目,我想实现用户的文本输入。它在起作用,但是很难阅读,而且不是很实用,所以我对其进行了重新设计。但是它停止了工作。我查找了互联网,发现此 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
box.inputcheck(event)
在事件循环后发射。事件循环可以一次性地持有多个事件,例如
textInput
和keydown
。当InputCheck
调用时,它将最后一个事件作为参数传递。解决方案是将
box.inputcheck
移动到事件循环中: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
andKeyDown
. WheninputCheck
calls it passes the last event as the argument.A solution would be to move
box.inputCheck
into the event loop: