微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[NSImage 窗口]:无法识别的选择器发送到 PyObjC 中的实例 0x7fbb246a9e10

如何解决[NSImage 窗口]:无法识别的选择器发送到 PyObjC 中的实例 0x7fbb246a9e10

我正在尝试通过在 Python 中调用 .addSubvew_(image) 方法将图像添加到 NSPanel,但我一直收到 [NSImage window]: unrecognized selector sent to instance 0x7fbb246a9e10。我不确定问题是否是图像的初始化问题,或者我是否错误添加了子视图。但似乎初始化工作得很好,因为它在单独运行时不会出现任何错误。 完整代码如下:

from Cocoa import NSObject,NSApplication,NSApp,NSWindow,NSPanel,NSColor,NSImage,NSSound
from PyObjCTools import AppHelper

def main():
    NSApplication.sharedApplication()

    # we must keep a reference to the delegate object ourselves,# NSApp.setDelegate_() doesn't retain it. A local variable is
    # enough here.
    delegate = NSPanel.alloc().init()
    NSApp().setDelegate_(delegate)

    win = NSPanel.alloc()
    frame = ((875.0,140.0),(180.0,200.0))
    win.initWithContentRect_styleMask_backing_defer_(frame,9395,5)
    win.setLevel_(0)  # floating window

    image1 = NSImage.alloc()
    image1.initWithContentsOfFile_('/Users/yassine/Teams.png')
    win.contentView().addSubview_(image1)
    win.display()
    win.orderFrontRegardless()  # but this one does
    AppHelper.runEventLoop()

if __name__ == "__main__":
    main()

解决方法

NSView 的 addSubview: 方法添加了一个 view - 您需要为图像创建一个视图。使用 NSImageView 并替换您的图像语句将类似于:

#add NSImageView to the import
image = NSImage.alloc().initWithContentsOfFile_('/Users/yassine/Teams.png')
view = NSImageView.alloc().initWithFrame_(((10,10),(150.0,150.0)))
view.setImage_(image)
win.contentView().addSubview_(view)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。