如何在 PyObjC 选择器中表示 void 指针?

发布于 2024-07-19 14:57:43 字数 1333 浏览 3 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

英雄似剑 2024-07-26 14:57:43

我认为你根本不需要使用 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 约定是函数的首字母小写。

I think you don't need to use objc.selector at all; try this instead:

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

I've also found that I need to decorate the end-of-panel function with 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

Here's how I would write what you have:

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

Explanation of changes: I always use None rather than objc.nil and it hasn't messed me up yet; I don't think your panel needs to be a property of self since you get it in your return function; objc convention is to have the first letter of your function in lower case.

久随 2024-07-26 14:57:43

打开面板的正确方法是:

@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
        'openPanelDidEnd:returnCode:contextInfo:',    #didEndSelector
        None)    #contextInfo
    NSLog(u'openPanel started')

Dan 的代码也可以工作,但恕我直言,我的变体稍微清晰一些:您不传递实际的方法,而是传递应该调用的方法的名称。

The right way to open the panel is:

@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
        'openPanelDidEnd:returnCode:contextInfo:',    #didEndSelector
        None)    #contextInfo
    NSLog(u'openPanel started')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文