目前正在学习 Cybrary 的免费 Python 在线课程(我正在使用 3.6 进行编码),但我使用的是 Mac,而演示者使用的是 Windows。到目前为止,如果有的话,差异也很小。
然而,当前部分涉及学习和使用 Ctypes,“作业”说的是Write a function which takes two arguments, title and body and creates a MessageBox with those arguments
.
视频中使用的代码作为创建消息框的示例:
from ctypes import *
windll.user32.MessageBoxA(0, "Click Yes or No\n", "This is a title\n", 4)
My code:
# 2.1 Ctypes: Write a function which takes two arguments, title and body
# and creates a MessageBox with those arguments
def python_message_box(title, body):
return windll.user32.MessageBoxA(0, body, title, 0)
运行此命令会出现错误:
File ".../AdvancedActivities.py", line 9, in python_message_box
return windll.user32.MessageBoxA(0, body, title, 0)
NameError: name 'windll' is not defined
我不认为我需要说我在尝试运行时遇到了同样的错误
windll.user32.MessageBoxW(0, body, title, 0)
我无法在任何地方找到人们在 Mac 计算机上创建消息框的任何示例。这是 Windows 特有的功能吗?如果是这样,Mac 上的等效项是什么?
编辑:Mark Setchell 的解决方案是让 Python 运行终端函数来完成windll
任务,所以而不是windll.user32.MessageBoxA(0, body, title, 0)
, use:
command = "osascript -e 'Tell application \"System Events\" to
display dialog \""+body+"\"'"
system(command)