【问题标题】:Text input box in pygame/python [duplicate]pygame / python中的文本输入框[重复]
【发布时间】:2013-05-20 14:10:27
【问题描述】:

我正在使用 pygame/python 开发 RPG。我做了一个字符。允许您自定义播放器的创建者。现在我正在寻找一种在屏幕上提示输入名称的方法。我不希望它制作一个框,只需打印用户在特定区域输入的内容(见图)。 感谢您的帮助。

http://ubuntuone.com/3HdzOKroopUEf1YxqNnbFM

【问题讨论】:

    标签: python text input pygame


    【解决方案1】:

    您可以捕获事件,如果 event.type == KEYDOWN,则检查 event.key 以获取用户按下的键。然后您可以将其添加到文本变量中并在屏幕上显示。

    【讨论】:

      【解决方案2】:

      您也可以使用 EzText。它是一个基本上完成 FRJA 为您描述的模块。如果你用谷歌搜索“pygame 文本输入”,还有很多其他模块。下面是 EzText 的示例代码:

      # EzText example
      from pygame.locals import *
      import pygame, sys, eztext
      
      def main():
          # initialize pygame
          pygame.init()
          # create the screen
          screen = pygame.display.set_mode((640,240))
          # fill the screen w/ white
          screen.fill((255,255,255))
          # here is the magic: making the text input
          # create an input with a max length of 45,
          # and a red color and a prompt saying 'type here: '
          txtbx = eztext.Input(maxlength=45, color=(255,0,0), prompt='type here: ')
          # create the pygame clock
          clock = pygame.time.Clock()
          # main loop!
      
          while 1:
              # make sure the program is running at 30 fps
              clock.tick(30)
      
              # events for txtbx
              events = pygame.event.get()
              # process other events
              for event in events:
                  # close it x button si pressed
                  if event.type == QUIT: return
      
              # clear the screen
              screen.fill((255,255,255))
              # update txtbx
              txtbx.update(events)
              # blit txtbx on the sceen
              txtbx.draw(screen)
              # refresh the display
              pygame.display.flip()
      
      if __name__ == '__main__': main()
      

      【讨论】:

        【解决方案3】:

        我最近编写了另一个模块,可以更轻松地插入文本。您只需创建一个TextInput-object,然后在游戏的每一帧都为其提供事件,最后使用get_surface() 检索渲染的表面。

        这是一个演示如何使用它的示例程序:

        import pygame_textinput # Import the textinput-module
        import pygame
        pygame.init()
        
        # Create TextInput-object
        textinput = pygame_textinput.TextInput()
        
        screen = pygame.display.set_mode((1000, 200))
        clock = pygame.time.Clock()
        
        while True:
            screen.fill((225, 225, 225))
        
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    exit()
        
            # Feed it with events every frame
            textinput.update(events)
            # Blit its surface onto the screen
            screen.blit(textinput.get_surface(), (10, 10))
        
            pygame.display.update()
            clock.tick(30)
        

        如果您想在用户按下return 后处理用户输入,只需等待update()-方法返回True

        if textinput.update(events):
            foo()
        

        更详细的信息和源代码可以在[我的github页面](my github page.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-24
          • 2017-04-21
          • 2018-06-19
          • 2015-02-14
          • 2018-08-01
          • 2012-11-07
          • 2021-02-16
          • 2022-01-21
          相关资源
          最近更新 更多