如何使用Cocoa和Python(PyObjC)创建状态栏项目?

时间:2020-03-06 14:48:10  来源:igfitidea点击:

我已经在XCode中创建了一个全新的项目,并在AppDelegate.py文件中包含以下内容:

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中的所有其他代码都是默认的。

解决方案

我必须这样做才能使其正常工作:

  • 打开MainMenu.xib。确保应用程序委托的类为" MyApplicationAppDelegate"。我不确定我们是否必须这样做,但是我做到了。这是错误的,因此应用程序委托从来没有被调用过。
  • 添加statusItem.retain(),因为它会立即自动发布。

需要以上使用.retain()的方法,因为从applicationDidFinishLaunching()方法返回时,statusItem被销毁。改用self.statusItem将该变量绑定为MyApplicationAppDelegate实例中的字段。

这是一个不需要.xib / etc的修改示例。

from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper

start_time = NSDate.date()

class MyApplicationAppDelegate(NSObject):

    state = 'idle'

    def applicationDidFinishLaunching_(self, sender):
        NSLog("Application did finish launching.")

        self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
        self.statusItem.setTitle_(u"Hello World")
        self.statusItem.setHighlightMode_(TRUE)
        self.statusItem.setEnabled_(TRUE)

        # Get the timer going
        self.timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(start_time, 5.0, self, 'tick:', None, True)
        NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSDefaultRunLoopMode)
        self.timer.fire()

    def sync_(self, notification):
        print "sync"

    def tick_(self, notification):
        print self.state

if __name__ == "__main__":
    app = NSApplication.sharedApplication()
    delegate = MyApplicationAppDelegate.alloc().init()
    app.setDelegate_(delegate)
    AppHelper.runEventLoop()