【问题标题】:Pygame 'module' object has no attribute 'event'Pygame“模块”对象没有属性“事件”
【发布时间】:2018-06-28 17:25:52
【问题描述】:

我正在使用 pygame 让程序等待鼠标点击。我的代码是这样的:

import pygame
import pyautogui

done = True
while done:
    event = pygame.event.wait()
    if event.type == pygame.MOUSEBUTTONDOWN:
        pyautogui.hotkey("ctrlleft")
        done = True

但是我收到了这个错误

Traceback (most recent call last):
  File "C:/Users/WantedStarling/Desktop/pygame.py", line 1, in <module>
    import pygame
  File "C:/Users/WantedStarling/Desktop\pygame.py", line 5, in <module>
    event = pygame.event.wait()
AttributeError: 'module' object has no attribute 'event'`enter code here`

有人可以帮忙吗?

【问题讨论】:

    标签: pygame


    【解决方案1】:

    你不能将你的游戏命名为pygame.py,否则Python会认为你的游戏模块是真正的pygame模块,并且因为你的游戏没有event属性,它会引发AttributeError

    重命名您的模块以解决问题。

    【讨论】:

      【解决方案2】:

      在使用它的任何功能之前,您需要调用pygame.init()。在您的循环应该解决问题之前的任何地方调用它。

      仅供参考,您可以在一行中导入多个模块。

      import pygame, pyautogui
      pygame.init()
      

      【讨论】:

      • 现在我得到这个错误 AttributeError: 'module' object has no attribute 'init'
      • 您需要致电pygame.init() 是正确的,但这不是问题的原因。此外,建议您将导入放在单独的行中(请参阅PEP 8)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 2022-08-20
      • 2017-03-29
      • 2013-02-13
      • 2010-11-18
      相关资源
      最近更新 更多