从整数 id 获取 pyobjc 对象

编程入门 行业动态 更新时间:2024-10-28 11:28:32
本文介绍了从整数 id 获取 pyobjc 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-24 21:38:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1072492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   对象   id   pyobjc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!