微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame中的文本输入

如何解决pygame中的文本输入

我正在尝试在 pygame 中创建一个文本输入功能,我几乎成功了,但是出现了问题。

如果按下 Backspace 键多长时间它只会删除一个字符,它只会在我重新释放并再次按下 Backspace 键时删除一个字符。

我可以按一次退格键,它会每半秒连续删除一次字符,直到我重新松开退格键。

这是我的文本输入功能代码,但功能还没有完成。

def text_input(x,y,color,char=None,char_x=None,char_y=None,fill=(0,0),thing=None):
    """

    :param x: Defines the x axis or the width position where the text input should be drawn.
    :param y: Defines the y axis or the height position where the text input should be drawn.
    :param color: Defines the color of the text input.
    :param char: If the parameter char in not entered it does not do anything otherwise it display's the the parameter
                 char's text on the screen
    :param char_x: If the parameter char is entered char_x Defines the x axis or the width position where the text
                   should be drawn.
    :param char_y: If the parameter char is entered char_y Defines the y axis or the height position where the text
                   should be drawn.
    :param fill: If the parameter fill is entered it fills the display with the tuple of the rgb color pixels which the
                 user entered. But if the parameter fill is not entered it fills the display with the rgb color code
                 black.
    :param thing: If the user wants something to been drawn on the display the fill the parameter thing with the object
                  they want to draw. But if the parameter thing is not entered
    :return: It display's the text input on the window and the updates it
    """

    characters = ""

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

            elif detect.type == KEYDOWN:
                if detect.key == K_BACKSPACE:
                    characters = characters[:-1]
                    print(characters)
                elif detect.key == K_RETURN:
                    global text
                    text = characters
                    return

                else:
                    characters = characters + detect.unicode

                WINDOW.fill((0,0))
                text = FONT.render(f"{characters}",True,color)
                draw(text,x,True)

如果你想在这里知道我的完整代码是:

import pygame
from pygame.locals import *
from win32api import GetSystemMetrics
pygame.init()
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)-64
WIDTH_HEIGHT = (WIDTH,HEIGHT)

WINDOW = pygame.display.set_mode(WIDTH_HEIGHT)
FONT = pygame.font.Font("Password generator font.ttf",32)
text = ""


def draw(thing,update=None):
    """

    :param thing: The object what needs to be drawn on the screen
    :param x: Defines the x axis or the width position where the object should be drawn.
    :param y: Defines the y axis or the height position where the object should be drawn.
    :param update: If the parameter update is not passed we don't update the display.
                   But if the parameter update is passed as True we update the display.
    :return: It returns the object on the display at its specific place or it returns
            the object on the display at its specific place and updates and updates the display based on the parameter
            update.
    """

    if update is True:
        WINDOW.blit(thing,(x,y))
        return pygame.display.update()

    else:
        return WINDOW.blit(thing,y))


def text_input(x,True)


running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    text_input(0,(255,255,255))

解决方法

使用 pygame.key.set_repeat() 控制按住键的重复方式:

当启用键盘重复时,按下的键会产生多个 pygame.KEYDOWN 事件。 [...]

[...] 禁用键重复调用此函数,不带参数或延迟设置为 0。

只需调用 pygame.key.set_repeat() 即可禁用重复键。例如:

pygame.key.set_repeat(100,100)
,

正如Rabbid76所说,你需要添加

pygame.key.set_repeat(delay,interval)

代码。

您可以在开始时添加此内容,设置屏幕后,在以下行下:

text = ""

延迟和间隔以毫秒为单位,所以

pygame.key.set_repeat(100,100)

每 1/10 秒重复一次。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。