【问题标题】:'String' module object has no attribute 'join'“字符串”模块对象没有属性“加入”
【发布时间】:2013-07-03 15:21:32
【问题描述】:

所以,我想在 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()

【问题讨论】:

  • 希望有人知道如何解决这个问题

标签: python string textbox pygame


【解决方案1】:

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

string.join(current_string,"")

例如该行应该是

"".join(current_string)

current_string 是一个可迭代对象。

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

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

会给你一个由逗号和空格分隔的字母 a b 和 c 组成的 str 对象。

【讨论】:

  • 完美!!!我尝试了 current_string.join(""),但它从来没有用过,所以我放弃了。我正要尝试,只是从不打扰。现在我不敢相信我浪费了这么多时间。非常感谢。
【解决方案2】:

string.join(words[, sep])has been deprecated with Python 2.4,用Python 3.0完全删除

所以直到Python 2.7 一个都可以使用string.join(words[, sep])sep.join(words)(虽然第一个不推荐,因为Python 2.4),从Python 3.0 只有后者可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2017-03-29
    • 2013-02-13
    • 2010-11-18
    • 2019-01-03
    • 2017-05-19
    相关资源
    最近更新 更多