10.6中PyObjC中的openPanelDidEnd问题 [英] Problem With openPanelDidEnd in PyObjC in 10.6

查看:106
本文介绍了10.6中PyObjC中的openPanelDidEnd问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下在OS X 10.5下正常工作的代码在10.6上失败:

The following code which worked fine under OS X 10.5 now fails on 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>

推荐答案

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

As elv notes, beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo: has been deprecated in 10.6, and the new method to use is beginSheetModalForWindow:completionHandler: There's no metadata for this method in the version of PyObjC that shipped with Snow Leopard, but it has since been added, and you can update the appropriate file yourself so that you can use this method. Open /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/AppKit/PyObjC.bridgesupport and find the element:

<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.您可以为完成处理程序传递任何可调用的消息,只要它具有正确的签名即可(即采用整数参数且不返回任何内容).一个例子:

This is the metadata that the Python side needs in order to get and return the correct types of objects to Objective-C. You can pass any callable for the completion handler, as long as it has the correct signature (i.e., takes an integer argument and returns nothing). An example:

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'))

这篇关于10.6中PyObjC中的openPanelDidEnd问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆