Dav*_*idM 0 python cocoa pyobjc
我有一个用PyObjC编写的statusItem应用程序.statusItem有一个menuItem,它应该在单击时启动一个新窗口:
# Create statusItem
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
statusItem.setHighlightMode_(TRUE)
statusItem.setEnabled_(TRUE)
statusItem.retain()
# Create menuItem
menu = NSMenu.alloc().init()
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Preferences', 'launchPreferences:', '')
menu.addItem_(menuitem)
statusItem.setMenu_(menu)
Run Code Online (Sandbox Code Playgroud)
launchPreferences:方法是:
def launchPreferences_(self, notification):
preferences = Preferences.alloc().initWithWindowNibName_('Preferences')
preferences.showWindow_(self)
Run Code Online (Sandbox Code Playgroud)
Preferences是NSWindowController类:
class Preferences(NSWindowController):
Run Code Online (Sandbox Code Playgroud)
当我在XCode(Build&Go)中运行应用程序时,这很好用.但是,当我从XCode外部运行构建的.app文件时,statusItem和menuItem按预期显示,但是当我单击Preferences menuItem时,窗口不会出现.我已通过检查控制台输出验证了launchPreferences代码是否正在运行.
此外,如果我再次双击.app文件,则会出现窗口但如果我通过单击(例如,在Finder窗口上)更改活动窗口,则首选项窗口将消失.在我看来,这与活动窗口有关.
更新1 我尝试了这 两个答案,但都没有工作.如果我添加到launchPreferences方法:
preferences.makeKeyAndOrderFront_()
Run Code Online (Sandbox Code Playgroud)
要么
preferences.setLevel_(NSNormalWindowLevel)
Run Code Online (Sandbox Code Playgroud)
那我只是得到一个错误:
'Preferences'对象没有属性
您需要向应用程序发送activateIgnoringOtherApps:消息,然后发送窗口makeKeyAndOrderFront:.
在Objective-C中,这将是:
[NSApp activateIgnoringOtherApps:YES];
[[self window] makeKeyAndOrderFront:self];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1285 次 |
| 最近记录: |