objective-c - [NSImage 窗口]:无法识别的选择器发送到 PyObjC 中的实例 0x7fbb246a9e10
问题描述
我正在尝试通过.addSubvew_(image)
在 Python 中调用该方法将图像添加到 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, 0, 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:
方法添加了一个视图——你需要为图像创建一个视图。使用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)
推荐阅读
- javascript - 数组到字符串的转换 - React js
- javascript - 用于连接和设置没有像 xampp 这样的应用程序的用户数据库的代码
- jpa - 未使用实体管理器创建实体表
- git - 将 git-svn 存储库从 Linux(wsl) 复制到 Windows
- react-native - React Native - 检查 Screen 的渲染方法
- java - 自动添加千位分隔符作为数字输入不起作用
- r - 如何直接从网络加载 R .rds 文件?
- excel - 提交后如何从IE中提取数据
- java - 使用 Lombok RequiredArgsConstructor 解决歧义
- javascript - 无法将 vuex 存储数据传递给图形