如何在 PyObjC 选择器中表示 void 指针?
我想将 NSOpenPanel 用于我正在设计的应用程序。 这是我到目前为止所得到的:
@objc.IBAction
def ShowOpenPanel_(self, sender):
self.panel = NSOpenPanel.openPanel()
self.panel.setCanChooseFiles_(False)
self.panel.setCanChooseDirectories_(True)
NSLog(u'Starting OpenPanel')
self.panel.beginForDirectory_file_types_modelessDelegate_didEndSelector_contextInfo_(
self.defaults.objectForKey_(u'projpath'),
objc.nil,
objc.nil,
self,
objc.selector(self.OpenPanelDidEnd_returnCode_contextInfo_,
signature='v:@ii'),
objc.nil)
NSLog(u'OpenPanel was started.')
def OpenPanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
NSLog('Panel ended.')
if (returnCode == NSOKButton):
NSLog(u'User selected OK')
path = self.panel.filenames()[0]
self.defaults.setObject_forKey_(path, u'projpath')
del self.panel
我关心的主要两行是:
objc.selector(self.OpenPanelDidEnd_returnCode_contextInfo_,
signature='v:@ii'),
objc.nil) #this is the argument that gets passed as the void pointer
第三个参数应该是一个 void 指针。 由于我不打算使用该数据,因此我宁愿将其留空。 我尝试过制作签名 'v:@iv'
并尝试使用 objc.NULL
和 python 的 None
以及几乎所有组合所有这些事情。 处理这个问题的最佳方法是什么?
I'm wanting to use an NSOpenPanel for an application I'm designing. Here's what I have so far:
@objc.IBAction
def ShowOpenPanel_(self, sender):
self.panel = NSOpenPanel.openPanel()
self.panel.setCanChooseFiles_(False)
self.panel.setCanChooseDirectories_(True)
NSLog(u'Starting OpenPanel')
self.panel.beginForDirectory_file_types_modelessDelegate_didEndSelector_contextInfo_(
self.defaults.objectForKey_(u'projpath'),
objc.nil,
objc.nil,
self,
objc.selector(self.OpenPanelDidEnd_returnCode_contextInfo_,
signature='v:@ii'),
objc.nil)
NSLog(u'OpenPanel was started.')
def OpenPanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
NSLog('Panel ended.')
if (returnCode == NSOKButton):
NSLog(u'User selected OK')
path = self.panel.filenames()[0]
self.defaults.setObject_forKey_(path, u'projpath')
del self.panel
The main two lines I'm concerned about are:
objc.selector(self.OpenPanelDidEnd_returnCode_contextInfo_,
signature='v:@ii'),
objc.nil) #this is the argument that gets passed as the void pointer
The third argument is supposed to be a void pointer. Since I don't intend to use that data, I'd rather just leave it empty. I've tried making the signature 'v:@iv'
and tried using objc.NULL
and python's None
, and just about every combination of all these things. What is the best way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你根本不需要使用 objc.selector ; 尝试这个:
我还发现我需要用
PyObjCTools.AppHelper.endSheetMethod
装饰面板结束函数:这是我将如何编写您所拥有的内容:
更改说明:我总是使用
None
而不是objc.nil
并且它还没有让我感到困惑; 我认为你的面板不需要是 self 的属性,因为你在返回函数中得到了它; objc 约定是函数的首字母小写。I think you don't need to use
objc.selector
at all; try this instead:I've also found that I need to decorate the end-of-panel function with
PyObjCTools.AppHelper.endSheetMethod
:Here's how I would write what you have:
Explanation of changes: I always use
None
rather thanobjc.nil
and it hasn't messed me up yet; I don't think your panel needs to be a property ofself
since you get it in your return function; objc convention is to have the first letter of your function in lower case.打开面板的正确方法是:
Dan 的代码也可以工作,但恕我直言,我的变体稍微清晰一些:您不传递实际的方法,而是传递应该调用的方法的名称。
The right way to open the panel is:
Dan's code works as well, but IMHO my variant is slighly clearer: you don't pass the actual method but the name of the method that should be called.