6

所以,我想在 Pygame 中创建一个用户文本输入框,我被告知要查看一个名为 inputbox 的类模块。所以我下载了 inputbox.py 并导入到我的主游戏文件中。然后我在其中运行了一个函数并得到了一个错误:

Traceback (most recent call last):
File "C:\Users\Dennis\Tournament\inputbox.py", line 64, in <module>
if __name__ == '__main__': main()
 File "C:\Users\Dennis\Tournament\inputbox.py", line 62, in main
print(ask(screen, "Name") + " was entered")
 File "C:\Users\Dennis\Tournament\inputbox.py", line 46, in ask
display_box(screen, question + ": " + string.join(current_string,""))
AttributeError: 'module' object has no attribute 'join'

我尝试单独运行 inputbox.py 并得到同样的错误。我正在使用 Python 3.3 和 Pygame 3.3,所以这可能是一个问题。有人告诉我,最近删除了许多“字符串”函数。如果有人知道问题是什么并且可以解决它,那么这里是代码:如果有人可以解决问题,我将非常感激,因为我一直在尝试在我的 Pygame 游戏中设置用户输入很长时间了。非常感谢您提前回答。

# by Timothy Downs, inputbox written for my map editor

# This program needs a little cleaning up
# It ignores the shift key
# And, for reasons of my own, this program converts "-" to "_"

# A program to get user input, allowing backspace etc
# shown in a box in the middle of the screen
# Called by:
# import inputbox
# answer = inputbox.ask(screen, "Your name")
#
# Only near the center of the screen is blitted to

import pygame, pygame.font, pygame.event, pygame.draw, string
from pygame.locals import *

def get_key():
  while 1:
    event = pygame.event.poll()
    if event.type == KEYDOWN:
      return event.key
    else:
      pass

def display_box(screen, message):
  "Print a message in a box in the middle of the screen"
  fontobject = pygame.font.Font(None,18)
  pygame.draw.rect(screen, (0,0,0),
                   ((screen.get_width() / 2) - 100,
                    (screen.get_height() / 2) - 10,
                    200,20), 0)
  pygame.draw.rect(screen, (255,255,255),
                   ((screen.get_width() / 2) - 102,
                    (screen.get_height() / 2) - 12,
                    204,24), 1)
  if len(message) != 0:
    screen.blit(fontobject.render(message, 1, (255,255,255)),
                ((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10))
  pygame.display.flip()

def ask(screen, question):
  "ask(screen, question) -> answer"
  pygame.font.init()
  current_string = []
  display_box(screen, question + ": " + string.join(current_string,""))
  while 1:
    inkey = get_key()
    if inkey == K_BACKSPACE:
      current_string = current_string[0:-1]
    elif inkey == K_RETURN:
      break
    elif inkey == K_MINUS:
      current_string.append("_")
    elif inkey <= 127:
      current_string.append(chr(inkey))
    display_box(screen, question + ": " + string.join(current_string,""))
  return string.join(current_string,"")

def main():
  screen = pygame.display.set_mode((320,240))
  print(ask(screen, "Name") + " was entered")

if __name__ == '__main__': main()
4

2 回答 2

10

当您应该从 str 对象中使用它时,您正在尝试使用来自 string 模块的 join 方法。

string.join(current_string,"")

例如,该行应该是

"".join(current_string)

其中 current_string 是可迭代的。

只是一个关于 .join 方法如何工作的简单示例

", ".join(['a','b','c'])

将为您提供由逗号和空格分隔的字母 ab 和 c 的 str 对象。

于 2013-07-03T15:26:52.853 回答
0

string.join(words[, sep]) 已被弃用Python 2.4,完全被移除Python 3.0

所以最多Python 2.7一个可以同时使用string.join(words[, sep])and sep.join(words)(尽管第一个不推荐使用Python 2.4),并且Python 3.0只有后者可用。

于 2020-04-28T07:19:10.703 回答