gpt4 book ai didi

python - 如何在 PyObjC 选择器中表示空指针?

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:45 35 4
gpt4 key购买 nike

我想为我正在设计的应用程序使用 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

第三个参数应该是一个空指针。由于我不打算使用该数据,因此我宁愿将其留空。我尝试制作签名 'v:@iv' 并尝试使用 objc.NULL 和 python 的 None,以及几乎所有组合所有这些事情。处理此问题的最佳方法是什么?

最佳答案

我认为你根本不需要使用objc.selector;试试这个:

@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,
self.OpenPanelDidEnd_returnCode_contextInfo_,
objc.nil)
NSLog(u'OpenPanel was started.')

我还发现我需要用 PyObjCTools.AppHelper.endSheetMethod 修饰面板结束函数:

@PyObjCTools.AppHelper.endSheetMethod
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.IBAction
def showOpenPanel_(self, sender):
panel = NSOpenPanel.openPanel()
panel.setCanChooseFiles_(False)
panel.setCanChooseDirectories_(True)
NSLog(u'Starting openPanel')
panel.beginForDirectory_file_types_modelessDelegate_didEndSelector_contextInfo_(
self.defaults.objectForKey_(u'projpath'), #forDirectory
None, #file
None, #types
self, #modelessDelegate
self.openPanelDidEnd_returnCode_contextInfo_, #didEndSelector
None) #contextInfo
NSLog(u'openPanel started')

@PyObjCTools.AppHelper.endSheetMethod
def openPanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
NSLog(u'Panel ended')
if returnCode != NSOKButton:
return
NSLog(u'User selected OK')
path = panel.filenames()[0]
self.defaults.setObject_forKey_(path, u'projpath')

更改说明:我总是使用 None 而不是 objc.nil 并且它还没有把我搞砸;我认为您的面板不需要是 self 的属性,因为您在返回函数中获得了它; objc 惯例是将函数的首字母小写。

关于python - 如何在 PyObjC 选择器中表示空指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/845970/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com