0

以下在 OS X 10.5 下运行良好的代码现在在 10.6 上失败:

    @IBAction
def addButton_(self, sender):
    panel = NSOpenPanel.openPanel()
    panel.setCanChooseDirectories_(YES)
    panel.setAllowsMultipleSelection_(YES)
    try:
        panel.beginSheetForDirectory_file_modalForWindow_modalDelegate_didEndSelector_contextInfo_(self.directory, None, NSApp().mainWindow(), self, 'openPanelDidEnd:panel:returnCode:contextInfo:', None)
    except:
        pass

@AppHelper.endSheetMethod
def openPanelDidEnd_panel_returnCode_contextInfo_(self, panel, returnCode, contextInfo):

我得到的错误是:

objc.BadPrototypeError: Python signature doesn't match implied Objective-C signature for <unbound selector openPanelDidEnd:panel:returnCode:contextInfo: of controller at 0x6166a70>
4

2 回答 2

1

正如 elv 所指出的,beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo:在 10.6 中已弃用,使用的新方法是beginSheetModalForWindow:completionHandler:Snow Leopard 附带的 PyObjC 版本中没有此方法的元数据,但它已被添加,您可以自己更新相应的文件你可以使用这个方法。打开 /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/AppKit/PyObjC.bridgesupport 并找到元素:

<class name='NSSavePanel'>

在其中,添加以下内容:

<method selector='beginSheetModalForWindow:completionHandler:'>
    <arg index='1' block='true' >
        <retval type='v' />
        <arg type='i' type64='q' />
    </arg>
</method>
<method selector='beginWithCompletionHandler:'>
    <arg index='0' block='true' >
        <retval type='v' />
        <arg type='i' type64='q' />
    </arg>
</method>

这是 Python 端获取正确类型的对象并将其返回给 Objective-C 所需的元数据。您可以为完成处理程序传递任何可调用对象,只要它具有正确的签名(即,接受一个整数参数并且不返回任何内容)。一个例子:

def showOpenPanel_(self, sender):
    openPanel = NSOpenPanel.openPanel()

    def openPanelDidClose_(result):
        if result == NSFileHandlingPanelOKButton:
            openPanel.orderOut_(self)
            image = NSImage.alloc().initWithContentsOfFile_(openPanel.filename())
            self.imgView.setImage_(image)
    openPanel.setAllowedFileTypes_(NSImage.imageFileTypes())
    openPanel.beginSheetModalForWindow_completionHandler_(self.imgView.window(), 
                                                          objc.selector(openPanelDidClose_, argumentTypes='l'))
于 2011-03-11T11:02:09.783 回答
1

beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo: has been deprecated in 10.6: http://developer.apple.com/library/mac/#documentation/cocoa/reference/ApplicationKit/Classes/NSOpenPanel_Class/DeprecationAppendix/AppendixADeprecatedAPI.html

struggling on the same problem cause PyObjC has no block signature http://pyobjc.sourceforge.net/documentation/pyobjc-core/blocks.html for beginSheetModalForWindow:completionHandler: and you can only use runModal

my solution:

panel = NSOpenPanel.openPanel()
panel.setCanChooseDirectories_(NO)
panel.setAllowsMultipleSelection_(NO)

panel.setAllowedFileTypes_(self.filetypes)
panel.setDirectoryURL_(os.getcwd())

ret = panel.runModal()
if ret:
    print panel.URL()

panel.URL() returns the user selection.

于 2011-03-02T01:17:48.723 回答