为什么按回车键时K_RETURN事件没有激活?



在尝试使用pygame模块创建一个小输入窗口时,我能够创建一个小矩形,在其中引入文本。这个想法是每当我按下Enter键,它会打印文本到窗口,但它没有…当我按下回车键时,它甚至不识别回车键,而是试图输入它。

import sys, pygame
from pygame.constants import KEYDOWN
import string
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([800,800])
base_font = pygame.font.Font(None, 32)
user_text = ' '
input_rect = pygame.Rect(200, 200, 140, 32)
color_selected = pygame.Color('lightskyblue3')
color_non_selected = pygame.Color(255, 255, 255)
color = color_non_selected
textbox_selected = False
text_rect = pygame.Rect(600, 200, 140, 32)
print_text = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if input_rect.collidepoint(event.pos):
textbox_selected = True
else:
textbox_selected = False
if event.type == pygame.KEYDOWN:
if textbox_selected == True:
if event.key == pygame.K_BACKSPACE:
user_text = user_text[:-1]
else:
user_text += event.unicode
if event.key == pygame.K_RETURN:
if user_text[0] in string.ascii_letters:
print_text = True
else:
print_text = False

screen.fill((0,0,0))
# Code for selecting, unselecting the box. Also makes the text to not get out
# from the box when typing too much
if textbox_selected:
color = color_selected
else:
color = color_non_selected
pygame.draw.rect(screen, color, input_rect, 2)
text_input = base_font.render(user_text, True, (255, 255, 255))
screen.blit(text_input, (input_rect.x + 5, input_rect.y + 5))

input_rect.w = max(250, text_input.get_width() + 15)
#Code for checking if there is text and if so, print it.
if print_text:
text_showed = base_font.render(user_text, True, (255, 255, 255))
screen.blit(text_showed, (text_rect.x, text_rect.y))
print_text = False
else: 
pass

pygame.display.flip()
clock.tick(60)    

谁能帮帮我,我好像找不到我的错误:/。我使用python 3.8

您的问题是,在if print_text:内部,您再次设置print_text = False,因此此文本仅在一个循环中显示,该循环采用16 milliseconds(因为您有clock.tick(60)1/60 second = 16 millisecons),文本被删除得非常快,您看不到它。

你应该用完全不同的方法去做。您应该使用print_text来显示文本(您应该在所有循环中进行blit),RETURN应该将user_text的文本复制到print_text

if event.key == pygame.K_RETURN:
print_text = user_text

当然,如果print_text为空,你可以跳过它

if print_text:  # it checks if `print_text` is not empty
text_showed = base_font.render(print_text, True, (255, 255, 255))
screen.blit(text_showed, (text_rect.x, text_rect.y))

但不要将print_text再次设置为旧值。


顺便说一句:

您可以在选中KEY时使用if/elif/else,这样您可以从显示的字符串中跳过RETURN

allowed_keys = string.ascii_letters  # only text
#allowed_keys = string.digits         # only integer number
#allowed_keys = "01"                  # only binary number
#allowed_keys = None                  # all keys allowed

if event.key == pygame.K_BACKSPACE:
user_text = user_text[:-1]
elif event.key == pygame.K_RETURN:
print_text = user_text
else:
# filter keys
if (allowed_keys is None) or (event.unicode in allowed_keys):
user_text += event.unicode

完整工作代码

import sys     # PEP8: every import in new line
import string  # PEP8: standard modules before other modules
import pygame
#from pygame.constants import KEYDOWN  # you don't need it because you use `pygame.KEYDOWN`
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([800,800])
base_font = pygame.font.Font(None, 32)
user_text = ''  # text in input widget
input_rect = pygame.Rect(200, 200, 140, 32)
color_selected = pygame.Color('lightskyblue3')
color_non_selected = pygame.Color(255, 255, 255)
color = color_non_selected
textbox_selected = False
text_rect = pygame.Rect(600, 200, 140, 32)
print_text = ''  # displayed text
allowed_keys = string.ascii_letters  # only text
#allowed_keys = string.digits         # only integer number
#allowed_keys = "01"                  # only binary number
#allowed_keys = None                  # all keys allowed
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if input_rect.collidepoint(event.pos):
textbox_selected = True
else:
textbox_selected = False
if event.type == pygame.KEYDOWN:
if textbox_selected == True:
if event.key == pygame.K_BACKSPACE:
user_text = user_text[:-1]
elif event.key == pygame.K_RETURN:
print_text = user_text
else:
# filter other keys
if (allowed_keys is None) or (event.unicode in allowed_keys):
user_text += event.unicode
screen.fill((0,0,0))
# Code for selecting, unselecting the box. Also makes the text to not get out
# from the box when typing too much
if textbox_selected:
color = color_selected
else:
color = color_non_selected
pygame.draw.rect(screen, color, input_rect, 2)
text_input = base_font.render(user_text, True, (255, 255, 255))
screen.blit(text_input, (input_rect.x + 5, input_rect.y + 5))

input_rect.w = max(250, text_input.get_width() + 15)
#Code for checking if there is text and if so, print it.
if print_text:   # it checks if `print_text` is not empty
text_showed = base_font.render(print_text, True, (255, 255, 255))
screen.blit(text_showed, (text_rect.x, text_rect.y))
pygame.display.flip()
clock.tick(60)    

PEP 8——Python代码风格指南

最新更新