有没有一种方法可以获取Objective-C对象的PyObjC代理对象,仅将其id作为整数?没有扩展模块可以完成吗?
id
就我而言,我试图从返回值wx.Frame.GetHandle(使用wxMac和Cocoa)中获取一个Cocoa代理对象。
wx.Frame.GetHandle
我确实找到的一种解决方案依赖于ctypes从创建一个objc_object对象id:
ctypes
objc_object
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:
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函数或其他更巧妙的方法,我肯定会感兴趣。
pyobjc