为啥我不能在我的 pygame 文本框中输入超过 1 个字母? [复制]

Posted

技术标签:

【中文标题】为啥我不能在我的 pygame 文本框中输入超过 1 个字母? [复制]【英文标题】:Why can't I type more than 1 letter into my pygame textboxes? [duplicate]为什么我不能在我的 pygame 文本框中输入超过 1 个字母? [复制] 【发布时间】:2021-09-06 14:07:24 【问题描述】:

我希望用户在单击两个文本框时能够在每个文本框中输入一个名称。相反,下一个字母将始终替换前一个字母,而不是在屏幕上写出一个新字母。例如,如果您尝试键入“mat”,则 a 将替换 m,t 将替换 a。我该如何解决这个问题?

from pygame import *

screen_width = 1400
screen_height = 800

init()
screen = display.set_mode((screen_width, screen_height))

name_font = font.Font(None, 32)
name_1 = ""
name_2 = ""


class Rectangle:

    def __init__(self, x, y):
        self.name = ""
        self.active = False
        self.x = x
        self.y = y

        self.text_surface = name_font.render(self.name, True, (255, 255, 255))
        self.rect_width = max(140, 10 + self.text_surface.get_width())
        self.input_rect = Rect(x, y, self.rect_width, 32)
        self.input_rect.w = self.text_surface.get_width() + 10
        self.click_value = 10

    def naming(self, player_name):

        for e in events:
            if e.type == MOUSEBUTTONDOWN:
                if self.input_rect.x + self.click_value >= mx >= self.input_rect.x - 10 and self.input_rect.y + 26 >= my >= self.input_rect.y - 10:
                    self.active = True
                else:
                    self.active = False

            if e.type == KEYDOWN:
                if self.active:
                    if e.key == K_BACKSPACE:
                        player_name = player_name[:-1]
                        screen.fill((0, 0, 0))
                    else:
                        player_name += e.unicode
                        self.click_value += 7

                    self.text_surface = name_font.render(player_name, True, (255, 255, 255))

    def draw(self, screen):
        draw.rect(screen, (0, 0, 0), self.input_rect, 0)
        draw.rect(screen, (255, 255, 255), self.input_rect, 2)
        self.input_rect.w = self.text_surface.get_width() + 10
        screen.blit(self.text_surface, (self.input_rect.x + 5, self.input_rect.y + 5))


rect_1 = Rectangle(200, 200)

rect_2 = Rectangle(200, 400)

while True:
    mx, my = mouse.get_pos()

    events = event.get()
    for e in events:
        if e.type == QUIT:
            quit()

    screen.fill((0, 0, 0))
    rect_1.naming(name_1)
    rect_1.draw(screen)

    rect_2.naming(name_2)
    rect_2.draw(screen)

    display.update()
    time.delay(1)

我认为这与 name_1 和 name_2 变量的位置有关,但我不完全确定。

【问题讨论】:

【参考方案1】:

Pyhton 没有输入输出参数的概念。使用self.name 属性,而不是参数:

from pygame import *

screen_width = 1400
screen_height = 800

init()
screen = display.set_mode((screen_width, screen_height))

name_font = font.Font(None, 32)
name_1 = ""
name_2 = ""

class Rectangle:

    def __init__(self, x, y):
        self.name = ""
        self.active = False
        self.x = x
        self.y = y

        self.text_surface = name_font.render(self.name, True, (255, 255, 255))
        self.rect_width = max(140, 10 + self.text_surface.get_width())
        self.input_rect = Rect(x, y, self.rect_width, 32)
        self.input_rect.w = self.text_surface.get_width() + 10
        self.click_value = 10

    def naming(self):

        for e in events:
            if e.type == MOUSEBUTTONDOWN:
                if self.input_rect.x + self.click_value >= mx >= self.input_rect.x - 10 and self.input_rect.y + 26 >= my >= self.input_rect.y - 10:
                    self.active = True
                else:
                    self.active = False

            if e.type == KEYDOWN:
                if self.active:
                    if e.key == K_BACKSPACE:
                        self.name = self.name[:-1]
                        screen.fill((0, 0, 0))
                    else:
                        self.name += e.unicode
                        self.click_value += 7

                    self.text_surface = name_font.render(self.name, True, (255, 255, 255))
        return self.name

    def draw(self, screen):
        draw.rect(screen, (0, 0, 0), self.input_rect, 0)
        draw.rect(screen, (255, 255, 255), self.input_rect, 2)
        self.input_rect.w = self.text_surface.get_width() + 10
        screen.blit(self.text_surface, (self.input_rect.x + 5, self.input_rect.y + 5))


rect_1 = Rectangle(200, 200)

rect_2 = Rectangle(200, 400)

while True:
    mx, my = mouse.get_pos()

    events = event.get()
    for e in events:
        if e.type == QUIT:
            quit()

    screen.fill((0, 0, 0))
    name_1 = rect_1.naming()
    rect_1.draw(screen)

    name_2 = rect_2.naming()
    rect_2.draw(screen)

    display.update()
    time.delay(1)

【讨论】:

【参考方案2】:

您可以使用此代码:-

import pygame
import sys

pygame.init()

clock = pygame.time.Clock()

# it will display on screen
screen = pygame.display.set_mode([600, 500])

# basic font for user typed
base_font = pygame.font.Font(None, 32)
user_text = ''

# create rectangle
input_rect = pygame.Rect(200, 200, 140, 32)

# color_active stores color(lightskyblue3) which
# gets active when input box is clicked by user
color_active = pygame.Color('lightskyblue3')

# color_passive store color(chartreuse4) which is
# color of input box.
color_passive = pygame.Color('chartreuse4')
color = color_passive

active = False

while True:
    for event in pygame.event.get():

    # if user types QUIT then the screen will close
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False

        if event.type == pygame.KEYDOWN:

            # Check for backspace
            if event.key == pygame.K_BACKSPACE:

                # get text input from 0 to -1 i.e. end.
                user_text = user_text[:-1]

            # Unicode standard is used for string
            # formation
            else:
                user_text += event.unicode
    
    # it will set background color of screen
    screen.fill((255, 255, 255))

    if active:
        color = color_active
    else:
        color = color_passive
        
    # draw rectangle and argument passed which should
    # be on screen
    pygame.draw.rect(screen, color, input_rect)

    text_surface = base_font.render(user_text, True, (255, 255, 255))
    
    # render at position stated in arguments
    screen.blit(text_surface, (input_rect.x+5, input_rect.y+5))
    
    # set width of textfield so that text cannot get
    # outside of user's text input
    input_rect.w = max(100, text_surface.get_width()+10)
    
    # display.flip() will update only a portion of the
    # screen to updated, not full area
    pygame.display.flip()
    
    # clock.tick(60) means that for every second at most
    # 60 frames should be passed.
    clock.tick(60)

【讨论】:

以上是关于为啥我不能在我的 pygame 文本框中输入超过 1 个字母? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 pygame 应用程序循环不能正常工作? [复制]

我怎样才能只获得在我的文本框中输入的小时数,并与在 rateperhour 中的 keyup 输入相乘,然后在我的总数中显示答案?

为啥当我使用 Pygame 时 Spyder 不能在任何地方显示我的图形输出?

当我在我的文本框中输入故事时,它应该点击这个 api

自动更新文本框

为啥 PyGame 在延迟或睡眠之前没有在窗口中绘制?