在python中将键盘输入附加到变量

在python中将键盘输入附加到变量,python,pygame,append,Python,Pygame,Append,我希望变量text=[]附加键盘输入,然后打印它 import pygame pygame.init() screen = pygame.display.set_mode([600, 400]) keepGoing = True def get_text (): keepText = True while keepText: text = [] # I want to add input here for event in pygame.event.g

我希望变量
text=[]
附加键盘输入,然后打印它

import pygame
pygame.init()
screen = pygame.display.set_mode([600, 400])
keepGoing = True

def get_text ():
    keepText = True
    while keepText:
        text = [] # I want to add input here

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            keys = pygame.key.name(event.key)
            text.append(keys)
            print (text)
            if event.key == pygame.K_ESCAPE:
                keepText = False                   

  while keepGoing:                        
      for event in pygame.event.get():    
          if event.type == pygame.QUIT: 
              keepGoing = False
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_SPACE:
                  get_text ()

          pygame.display.update()  

pygame.quit() 
我该怎么做呢?

我想这就是你要找的。你会想要像这样的东西

while keepText:
    text = []
    text.append(raw_input('Input something from the keyboard: '))
在你的问题中,缩进看起来有问题,而你的while循环永远不会结束。

我想这就是你想要的。你会想要像这样的东西

while keepText:
    text = []
    text.append(raw_input('Input something from the keyboard: '))

但在您的问题中,缩进看起来有问题,while循环永远不会结束。

如果发生
pygame.KEYDOWN
事件,您可以将其
.unicode
属性添加到字符串或将其附加到列表中

import pygame as pg


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    text = ''

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                text += event.unicode
                print(text)

        screen.fill((30, 30, 30))

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

如果发生
pygame.KEYDOWN
事件,您可以将其
.unicode
属性添加到字符串或将其附加到列表中

import pygame as pg


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    text = ''

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                text += event.unicode
                print(text)

        screen.fill((30, 30, 30))

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

打印文本时会得到什么?打印文本时会得到什么?我如何使用模块pygame做到这一点?@ПаааПаааааааааааааа?是否要将输入添加到带有注释的行上#我要在此处添加输入,或稍后在for循环中添加输入?我要将键盘添加到变量。然后我将使用这个变量。我希望输入在pygame中工作。如何在模块pygame中进行输入?我想我不能再提供任何帮助,但希望@skrx将有所帮助。我如何在模块pygame中进行输入?@ПаааПааааааааааааа107?是否要将输入添加到带有注释的行上#我要在此处添加输入,或稍后在for循环中添加输入?我要将键盘添加到变量。然后我将使用这个变量。我希望输入在pygame中工作。如何在pygame模块中进行输入?我想我无法提供更多帮助,但希望@skrx提供的帮助。谢谢?这正是我想要的。谢谢?这正是我要找的。