python - 在 Mac OS 上运行 python 代码时出错
问题描述
我儿子刚开始在学校学习 python,他必须在 Mac OS 上编写一个简单的程序。
代码是这样的
import ctypes
def mBox(mode, message, title, style):
return(ctypes.windll.user32.MessageBoxW(mode, message, title, style))
MB_OK = 0
MB_OKCANCEL = 1
MB_ABORTRETRYIGNORE = 2
MB_YESNOCXL = 3
MB_RETRYNO = 5
MB_CANCELTRYAGAINCONTINUE = 6
MB_HELP = 16384
ICON_EXCLAIM = 48
ICON_INFO = 64
ICON_STOP = 16
IDOK = 0
IDCANCEL = 2
IDABORT = 3
IDYES = 6
IDNO = 7
result = mBox(None, "Do you know what you are doing?","Support",ICON_EXCLAIM | MB_HELP)
if result == IDYES:
print("user pressed Yes")
elif result == IDNO:
print("user pressed No")
elif result == IDCANCEL:
print("user pressed Cancel")
else:
print("unknown return code",result)
执行此代码时,将显示以下消息
Traceback (most recent call last):
File "/Users/KeeganBarretto/Documents/MESSAGE BOXES 5.py", line 25, in <module>
result = mBox(None, "Do you know what you are doing?","Support",ICON_EXCLAIM | MB_HELP)
File "/Users/KeeganBarretto/Documents/MESSAGE BOXES 5.py", line 5, in mBox
return(ctypes.windll.user32.MessageBoxW(mode, message, title, style))
AttributeError: module 'ctypes' has no attribute 'windll'
有人可以帮助他解决这个问题。正如我提到的,他是 python 的新手,学习是他计算机研究的一部分。
解决方案
该代码仅适用于 Windows,因为它是MessageBoxW
从 Windows调用的user32.dll
。
推荐阅读
- python - 将类的所有参数和对象发送到 PyTorch 中的同一设备
- java - 在 Java/Groovy 语言中“实现”基本工厂方法
- python-3.x - 我如何使用\n(新字符)在python字典中的每个键值对之间插入一个空行
- java - 误解或可能违反 Java 中的通用子类型规则
- angular - 如何为 Angular 项目中的所有 get 函数启用 CORS?
- javascript - 构建反应应用程序的版本,提供指向 CSS 和 JS 文件的错误链接
- python - 按位置,for循环,python从文本中获取某些项目
- swift - Apple Pencil 笔的颜色比给 PKInkingTool 的颜色深
- javascript - 时间表应用程序 node.js、express、postgresql 的删除和编辑按钮
- javascript - 如何在javascript中获取函数的结果?