问题描述
限时送ChatGPT账号..有没有办法获得一个Objective-C对象的PyObjC代理对象,只要它的id
是一个整数?可以不用扩展模块吗?
Is there a way to get a PyObjC proxy object for an Objective-C object, given only its id
as an integer? Can it be done without an extension module?
就我而言,我试图从 wx.Frame.GetHandle
的返回值中获取 Cocoa 代理对象(使用 wxMac 和 Cocoa).
In my case, I'm trying to get a Cocoa proxy object from the return value of wx.Frame.GetHandle
(using wxMac with Cocoa).
推荐答案
我找到的一个解决方案依赖于 ctypes
从 创建一个
:objc_object
对象id
The one solution I did find relies on ctypes
to create an objc_object
object from the id
:
import ctypes, objc
_objc = ctypes.PyDLL(objc._objc.__file__)
# PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
_objc.PyObjCObject_New.restype = ctypes.py_object
_objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
def objc_object(id):
return _objc.PyObjCObject_New(id, 0, 1)
用于打印 wx.Frame
的示例:
import wx
class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(350,200))
m_print = wx.Button(self, label="Print")
m_print.Bind(wx.EVT_BUTTON, self.OnPrint)
def OnPrint(self, event):
topobj = objc_object(top.GetHandle())
topobj.print_(None)
app = wx.App()
top = Frame(title="ObjC Test")
top.Show()
app.MainLoop()
它有点讨厌,因为它使用了ctypes
.如果有我忽略的 pyobjc
API 函数或其他更简洁的方法,我肯定会感兴趣.
It's a little nasty since it uses ctypes
. If there's a pyobjc
API function I overlooked or some other neater way to do it, I'd surely be interested.
这篇关于从整数 id 获取 pyobjc 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论