【问题标题】:Why do I get an AttributeError when calling a class in a seperate module?为什么在单独的模块中调用类时会出现属性错误?
【发布时间】:2017-06-29 15:07:15
【问题描述】:

我正在(尝试)使用 Python 2.7.12 在 Pygame 中重新创建 Mario 级别 1-1。这是两个相关文件:

main.py: http://pastebin.com/HXmBdJ2a

马里奥.py: http://pastebin.com/29xu1tMM
我的问题是:当我运行 main.py 时,解释器给了我这个错误信息:

Traceback (most recent call last):
  File "C:/path/to/main.py", line 6, in <module>
    import mario
  File "C:\path\to\mario.py", line 7, in <module>
    import main
  File "C:\path\to\main.py", line 47, in <module>
    game = Game()
  File "C:\path\to\main.py", line 26, in __init__
    self.main_loop()
  File "C:\path\to\main.py", line 39, in main_loop
    self.Mario = mario.Mario()
AttributeError: 'module' object has no attribute 'Mario'
Process finished with exit code 1


我很困惑,就像 mario.py 对马里奥课程一样。如果我尝试运行 mario.py,我会收到以下错误:

Traceback (most recent call last):
  File "C:/path/to/mario.py", line 7, in <module>
    import main
  File "C:\path\to\main.py", line 6, in <module>
    import mario
  File "C:\path\to\mario.py", line 12, in <module>
    sheet = pygame.image.load("../assets/images/MarioLuigiSprites.png").convert()
pygame.error: No video mode has been set
Process finished with exit code 1


谁能给我解释一下?
编辑:我通过添加来修复它:
import sys sys.path.insert(0, "scripts")
import mario

【问题讨论】:

  • 尝试导入 mario.Mario
  • 另外,请尝试这样做screen = pygame.display.set_mode((num, num)) 以避免出现无视频模式错误
  • 我试过 import mario.Mario,得到:ImportError: No module named Mario
  • 问题在于 Game 类没有名为 Mario 的属性,因为您没有在 Game 类的 __init__ 中定义它。要么在 Game 类的 __init__ 中有 self.Mario = mario.Mario(),要么只调用 Mario = mario.Mario()。另外,你为什么在mario.py 的末尾打电话给mario = Mario()?您应该尝试删除它。

标签: python python-2.7 pygame attributeerror


【解决方案1】:

在这种情况下,AttributeError: 'module' object has no attribute 'Mario' 来自 Game 类没有名为 Mario 的属性,因为您没有在游戏的 init 中定义它班级。为了清楚起见,您没有:

class Game(object):
    def __init__(self):
        self.fps = 60
        self.showfps = True
        self.clock = pygame.time.Clock()

        # Set FPS
        self.clock.tick(self.fps)

        self.Mario = mario.Mario()  # notice this line!!!
        .......
        .......

您可以在 Game 类的 __init__ 中使用 self.Mario = mario.Mario(),或者在您拥有的代码中仅调用 Mario = mario.Mario()。
这意味着保留你已经拥有的代码你应该这样做:

def main_loop(self):
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.closegame()
        Mario = mario.Mario()  # notice: it's not self.Mario....
        DISPLAYSURF.fill(const.BLACK)   

旁注:为什么在 mario.py 的末尾调用 mario = Mario()?尝试删除它。
如果您不使用main.py 中定义的任何方法或类,您可以删除mario.py 中的import main。

【讨论】:

  • 谢谢。现在,当我运行 main 时,我得到一个不同的错误: sheet = pygame.image.load("../assets/images/MarioLuigiSprites.png").convert() pygame.error: No video mode has been set
  • 您是否已经查看了@Nikhil Raghavendra 关于该视频问题的评论之一?如果你没看的话,再来这里:inventwithpython.com/pygamecheatsheet.png 和 stackoverflow.com/questions/18905989/…
  • 即使我没有在 pygame.display.set_mode() 中使用元组,我仍然会收到 no video mode has been set 错误。设置显示后尝试导入 mario.Mario,得到: ImportError: No module named Mario
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2020-09-20
相关资源
最近更新 更多