如何使用Cocoa和Python(PyObjC)创建状态栏项目? [英] How can I create a status bar item with Cocoa and Python (PyObjC)?
问题描述
我在XCode中创建了一个全新的项目,并在我的AppDelegate.py文件中有以下内容:
I have created a brand new project in XCode and have the following in my AppDelegate.py file:
from Foundation import *
from AppKit import *
class MyApplicationAppDelegate(NSObject):
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
statusItem.setTitle_(u"12%")
statusItem.setHighlightMode_(TRUE)
statusItem.setEnabled_(TRUE)
但是,当我启动应用程序时,不会出现状态栏项。 main.py和main.m中的所有其他代码都是默认的。
However, when I launch the application no status bar item shows up. All the other code in main.py and main.m is default.
推荐答案
-
打开MainMenu.xib。确保应用程序代理的类是
MyApplicationAppDelegate
。我不知道你是否必须这样做,但我做到了。
Open MainMenu.xib. Make sure the class of the app delegate is
MyApplicationAppDelegate
. I'm not sure if you will have to do this, but I did. It was wrong and so the app delegate never got called in the first place.
添加 statusItem.retain()
,因为它会立即自动释放。
Add statusItem.retain()
because it gets autoreleased right away.
这篇关于如何使用Cocoa和Python(PyObjC)创建状态栏项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!