gpt4 book ai didi

python - 'String' 模块对象没有属性 'join'

转载 作者:太空狗 更新时间:2023-10-30 00:50:51 25 4
gpt4 key购买 nike

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

最佳答案

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

string.join(current_string,"")

例如那一行应该是

"".join(current_string)

其中 current_string 是可迭代的。

关于 .join 方法如何工作的简单示例

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

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

关于python - 'String' 模块对象没有属性 'join',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17451942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com