所以,我想在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()
答案 0 :(得分:8)
当你应该从str对象使用它时,你正试图使用字符串模块中的join方法。
string.join(current_string,"")
该行例如应为
"".join(current_string)
其中current_string是可迭代的。
关于.join方法如何工作的简单示例
", ".join(['a','b','c'])
将为您提供字母a b和c的str对象,用逗号和空格分隔。
答案 1 :(得分:0)
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
起后者可用。