python-3.x Pygame输入框中使用Unicode的换行符

u0njafvf  于 2023-03-09  发布在  Python
关注(0)|答案(3)|浏览(206)

我在pygame中做了一个简单的输入框,你可以在这里输入任何东西,按退格键,然后按回车键添加一个换行符。我似乎不能添加一个换行符,因为我不知道怎么做。我希望能够在按回车键的时候添加一个换行符,并保持其他文本在它上面。
我试过使用text = text + "/n",但没有效果。
这是我的密码:

import pygame
pygame.init()

winheight = 600
winwidth = 1200
font = pygame.font.Font(None, 32)
input_box = pygame.Rect(50, 50, winwidth - 100, winheight - 100)
blue = (0, 0, 255)
Text = ''
Writing = True

win = pygame.display.set_mode((winwidth, winheight))
pygame.display.set_caption("Clogging Cuesheet Writer")

while Writing:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Writing = False
            break
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                print ("replace this with the command to make Text plus a newline")
            elif event.key == pygame.K_BACKSPACE:
                Text = Text[:-1]
            else:
                Text += event.unicode

    #clear screen, draw text, draw inputbox, then update
    win.fill((0, 0, 0))
    txt_surface = font.render(Text, True, blue)
    win.blit(txt_surface, (input_box.x+15, input_box.y+15))
    pygame.draw.rect(win, blue, input_box, 2)
    pygame.display.update()
pygame.quit()

请帮帮忙

elcex8rz

elcex8rz1#

这是因为您在“/n”中有一处打字错误。您真正需要的是“\n”:

if event.key == pygame.K_RETURN:
    print (text = text + '\n')
6ljaweal

6ljaweal2#

好了,兄弟,这是我的代码显示在一个窗口的宽度,高度尺寸的文本
我得到新行的方法是ord(event.unicode)是13

offsetX = 0
offsetY = 0
font = pygame.font.Font(None, 20)

tmp_text = 'hi Qdfhasdj' # here u add the text u need with tmp_text+=event.unicode

line_counter=0
while tmp_text!='':
    line_text = ''
    while tmp_text!='':
        word = tmp_text[0]
        if font.render(line_text+word, True, tmp_currentTextColor, tmp_currentColor).get_rect()[2]>width-20:
            break
        if ord(word) == 13: #for new line because the '\n' not working for some reason
            if len(tmp_text)==1: 
                tmp_text = ''
                break
            tmp_text = tmp_text[1:]    
            break
        line_text=line_text+word
        if len(tmp_text)==1: 
            tmp_text = ''
            break
        tmp_text = tmp_text[1:]
    
    line_text = font.render(line_text, True, tmp_currentTextColor, tmp_currentColor)
    screen.blit(line_text, [offsetX +padding[0]/2,offsetY + padding[1]/2 + 1.5*padding[1]*line_counter])

    line_counter+=1
epggiuax

epggiuax3#

pygame-ce开始,2.1.4pygame.Font.render支持多行文本,所以你所要做的就是在文本字符串中添加一个换行符:

import pygame

WIDTH, HEIGHT = 1200, 600
FPS = 60

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.Clock()
pygame.display.set_caption("Clogging Cuesheet Writer")

input_box = pygame.Rect(50, 50, WIDTH - 100, HEIGHT - 100)
font = pygame.Font(None, 32)
text = ""

running = True
while running:
    clock.tick(FPS)
    screen.fill("black")

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                text += "\n"  # this is the "command" to add
            elif event.key == pygame.K_BACKSPACE:
                text = text[:-1]
        elif event.type == pygame.TEXTINPUT:
            text += event.text

    # some modifications to `render`, you need to specify bg colour which can be None
    # that's the default value and then you can specify wraplength
    # more info in docs
    txt_surface = font.render(text, True, "blue", None, input_box.width - 30)
    screen.blit(txt_surface, (input_box.x + 15, input_box.y + 15))

    pygame.draw.rect(screen, "blue", input_box, 2)

    pygame.display.flip()

您还可以使用TEXTINPUT事件来处理文本输入,它还有一个特性,即当您按住键时,事件会在一定的延迟后重复发布。
此外,要安装最新版本的pygame-ce,只需执行以下步骤:

  1. pip uninstall pygame(为避免冲突,只有在已经安装了pygame的情况下才必须执行此操作)
  2. pip install pygame-ce
    1.享受全新的闪亮功能
    更多关于分叉的信息可以在这里找到:Pygame: Community Edition Announcement
    下面是特定版本的公告:Pygame-ce 2.1.4 release

相关问题