这是Python代码:
#!/usr/bin/python
import os
import sys
from Foundation import *
from AppKit import *
import objc
def setupWindowMenu(app):
windowMenu = NSMenu.alloc().initWithTitle_('Window')
windowMenu.retain()
menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Minimize', 'performMiniaturize:', 'm')
windowMenu.addItem_(menuItem)
windowMenuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Window', None, '')
windowMenuItem.setSubmenu_(windowMenu)
app.mainMenu().addItem_(windowMenuItem)
app.setWindowsMenu_(windowMenu)
app = NSApplication.sharedApplication()
mainMenu = NSMenu.alloc().init()
app.setMainMenu_(mainMenu)
setupWindowMenu(app)
app.finishLaunching()
app.updateWindows()
app.activateIgnoringOtherApps_(True)
app.run()
我得到这个输出:
$ python pyobjcdemo.py
2011-09-11 23:22:40.470 Python[21409:1507] _createMenuRef called with existing principal MenuRef already associated with menu
2011-09-11 23:22:40.471 Python[21409:1507] (
0 CoreFoundation 0x00007fff93acb986 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8f3b4d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff93acb7ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff93acb744 +[NSException raise:format:] + 116
4 AppKit 0x00007fff8a8841fb -[NSCarbonMenuImpl _createMenuRef] + 64
5 AppKit 0x00007fff8a883ae6 -[NSCarbonMenuImpl _instantiateCarbonMenu] + 148
6 AppKit 0x00007fff8a8588e9 -[NSApplication finishLaunching] + 878
7 AppKit 0x00007fff8a85827d -[NSApplication run] + 118
8 libffi.dylib 0x00007fff8cf29e7c ffi_call_unix64 + 76
9 ??? 0x00007fbd1b480d10 0x0 + 140450183253264
)
为什么?
这是什么意思?
为什么这个简单的 PyObjC 代码有任何 Carbon 依赖项?
最佳答案
错误是对 finishLaunching 的调用。只需将其删除,就可以消除错误。
关于为什么在 Cocoa 中看到 Carbon 类型,这只是代码层的问题。 Carbon 和 Cocoa 并不是两个完全独立的系统,它们是同一系统的 API。 (尽管较低的“碳”层通常被称为“核心”技术,例如 Core Graphics。)
关于objective-c - cocoa 异常: _createMenuRef called with existing principal MenuRef already associated with menu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7381345/