从Python调用Objective C函数? [英] calling Objective C functions from Python?

查看:355
本文介绍了从Python调用Objective C函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以从Python动态调用Objective C函数?

Is there a way to dynamically call an Objective C function from Python?

例如,在Mac上,我想调用这个Objective C函数

For example, On the mac I would like to call this Objective C function

[NSSpeechSynthesizer availableVoices]

无需预先编译任何特殊的Python包装器模块.

without having to precompile any special Python wrapper module.

推荐答案

自OS X 10.5起,OS X附带了 PyObjC 桥,一个Python-Objective-C桥.它使用 BridgeSupport 框架将Objective-C框架映射到Python.与MacRuby不同,PyObjC是一个经典的桥梁-每个ObjC对象的python端都有一个代理对象,反之亦然.但是,该桥是无缝连接的,并且可以用PyObjC编写整个应用程序(Xcode具有一些基本的PyObjC支持,您可以在上面的链接中从PyObjC SVN下载Xcode的应用程序和文件模板).许多人将其用于实用程序或应用程序脚本/插件. Apple的开发人员网站上还有一个介绍,用于通过PyObjC用Python开发Cocoa应用程序,这略显不足.的日期,但对您来说可能是一个很好的概述.

Since OS X 10.5, OS X has shipped with the PyObjC bridge, a Python-Objective-C bridge. It uses the BridgeSupport framework to map Objective-C frameworks to Python. Unlike, MacRuby, PyObjC is a classical bridge--there is a proxy object on the python side for each ObjC object and visa versa. The bridge is pretty seamless, however, and its possible to write entire apps in PyObjC (Xcode has some basic PyObjC support, and you can download the app and file templates for Xcode from the PyObjC SVN at the above link). Many folks use it for utilities or for app-scripting/plugins. Apple's developer site also has an introduction to developing Cocoa applications with Python via PyObjC which is slightly out of date, but may be a good overview for you.

在您的情况下,以下代码将调用[NSSpeechSynthesizer availableVoices]:

In your case, the following code will call [NSSpeechSynthesizer availableVoices]:

from AppKit import NSSpeechSynthesizer

NSSpeechSynthesizer.availableVoices()

返回

(
    "com.apple.speech.synthesis.voice.Agnes",
    "com.apple.speech.synthesis.voice.Albert",
    "com.apple.speech.synthesis.voice.Alex",
    "com.apple.speech.synthesis.voice.BadNews",
    "com.apple.speech.synthesis.voice.Bahh",
    "com.apple.speech.synthesis.voice.Bells",
    "com.apple.speech.synthesis.voice.Boing",
    "com.apple.speech.synthesis.voice.Bruce",
    "com.apple.speech.synthesis.voice.Bubbles",
    "com.apple.speech.synthesis.voice.Cellos",
    "com.apple.speech.synthesis.voice.Deranged",
    "com.apple.speech.synthesis.voice.Fred",
    "com.apple.speech.synthesis.voice.GoodNews",
    "com.apple.speech.synthesis.voice.Hysterical",
    "com.apple.speech.synthesis.voice.Junior",
    "com.apple.speech.synthesis.voice.Kathy",
    "com.apple.speech.synthesis.voice.Organ",
    "com.apple.speech.synthesis.voice.Princess",
    "com.apple.speech.synthesis.voice.Ralph",
    "com.apple.speech.synthesis.voice.Trinoids",
    "com.apple.speech.synthesis.voice.Vicki",
    "com.apple.speech.synthesis.voice.Victoria",
    "com.apple.speech.synthesis.voice.Whisper",
    "com.apple.speech.synthesis.voice.Zarvox"
)

(我的SL计算机上的桥接NSCFArray).

(a bridged NSCFArray) on my SL machine.

这篇关于从Python调用Objective C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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