elk*_*ow5 4 python macos ctypes messagebox
目前正在学习 Cybrary 的免费在线 Python 课程(我使用 3.6 进行编码),但我使用的是 Mac,而演示者使用的是 Windows。到目前为止,几乎没有差异(如果有的话)。
然而,当前部分涉及学习和使用 Ctypes,并且“作业”说 to 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)
Run Code Online (Sandbox Code Playgroud)
我的代码:
# 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)
Run Code Online (Sandbox Code Playgroud)
运行这个会产生错误:
File ".../AdvancedActivities.py", line 9, in python_message_box
return windll.user32.MessageBoxA(0, body, title, 0)
NameError: name 'windll' is not defined
Run Code Online (Sandbox Code Playgroud)
我不相信我需要说我在尝试运行时遇到了同样的错误
windll.user32.MessageBoxW(0, body, title, 0)
Run Code Online (Sandbox Code Playgroud)
我无法在任何地方找到任何人在 Mac 计算机上创建消息框的示例。它是 Windows 特定的功能吗?如果是这样,Mac 相当于什么?
编辑:Mark Setchell 的解决方案是让 Python 运行完成windll任务的终端函数,因此windll.user32.MessageBoxA(0, body, title, 0)使用:
command = "osascript -e 'Tell application \"System Events\" to
display dialog \""+body+"\"'"
system(command)
Run Code Online (Sandbox Code Playgroud)
如果您在任何 Mac 上的终端中输入以下内容,您将看到一个对话框:
osascript -e 'Tell application "System Events" to display dialog "Some Funky Message" with title "Hello Matey"'
Run Code Online (Sandbox Code Playgroud)
有关更多示例,请参见此处。
因此,只需使用 Python 子流程调用来运行该...子流程文档,或使用system().
没什么好安装的。没有依赖性。您还可以使用相同的技术向用户询问值、选择文件或目录并选择颜色。对话框都是 Mac 原生的——不是一些丑陋的模仿。
| 归档时间: |
|
| 查看次数: |
3187 次 |
| 最近记录: |