在Mac上的Python中创建MessageBox? [英] Create a MessageBox in Python for Mac?

查看:332
本文介绍了在Mac上的Python中创建MessageBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在通过Cybrary的Python免费在线课程(我在3.6中进行编码),但是在演示者使用Windows的同时我使用的是Mac.到目前为止,几乎没有差异.

Currently working through a free online class for Python from Cybrary (I'm coding in 3.6), but I use a Mac while the presenter uses Windows. So far, there have been very few differences if any.

但是,本节讨论学习和使用Ctypes,赋值"表示Write a function which takes two arguments, title and body and creates a MessageBox with those arguments.

The current section deals with learning and using Ctypes however, and the "assignment" says to Write a function which takes two arguments, title and body and creates a MessageBox with those arguments.

视频中用作创建消息框示例的代码:

The code used in the video as an example of creating a Message Box:

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)

运行此操作会出现错误:

Running this gives the error:

File ".../AdvancedActivities.py", line 9, in python_message_box
   return windll.user32.MessageBoxA(0, body, title, 0)
NameError: name 'windll' is not defined

我不认为我需要说同样的错误尝试运行

I don't believe I need to say that I get the same error trying to run

windll.user32.MessageBoxW(0, body, title, 0)


我在Mac计算机上创建消息框的任何地方都找不到任何示例.它是Windows特定的功能吗?如果是这样,在Mac上相当于什么?


I haven't been able to find any examples anywhere of people creating Message Boxes on Mac computers. Is it a Windows-specific function? If so, what would be the Mac equivalent of this?

Mark Setchell的解决方案是让Python运行可完成windll任务的终端功能,因此请使用

代替windll.user32.MessageBoxA(0, body, title, 0)

Mark Setchell's solution is to have Python run terminal functions that accomplish windll tasks, so instead of windll.user32.MessageBoxA(0, body, title, 0), use:

command = "osascript -e 'Tell application \"System Events\" to
           display dialog \""+body+"\"'"
system(command)

推荐答案

如果在任何Mac上的终端"中键入此命令,则会显示一个对话框:

If you type this into a Terminal on any Mac, you'll get a dialog box:

osascript -e 'Tell application "System Events" to display dialog "Some Funky Message" with title "Hello Matey"'

有关其他示例,请参见此处.

See here for further examples.

因此,只需使用Python子进程调用来运行该程序即可... 子进程文档,或使用system().

So, just use a Python subprocess call to run that... subprocess documentation, or use system().

没有要安装的内容.没有依赖关系.您也可以使用相同的技术向用户询问值,选择文件或目录并选择颜色.对话框都是Mac的本机对话框,而不是一些丑陋的模仿.

Nothing to install. No dependencies. You can also ask user for values, select files or directories and pick colours using the same technique. The dialog boxes are all native Mac ones - not some ugly imitation.

这篇关于在Mac上的Python中创建MessageBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆