Pergunta

Eu criei um novo projeto no Xcode e tem o seguinte no meu arquivo 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)

No entanto, quando eu iniciar o aplicativo Não Estado Shows Item barra para cima. Todo o outro código em main.py e main.m é padrão.

Foi útil?

Solução

Eu tive que fazer isso para torná-lo trabalho:

  1. Open MainMenu.xib. Certifique-se a classe do delegado aplicativo é MyApplicationAppDelegate. Eu não tenho certeza se você vai ter que fazer isso, mas eu fiz. Ele estava errado e por isso o delegado aplicativo nunca foi chamado em primeiro lugar.

  2. Add statusItem.retain() porque fica autoreleased imediatamente.

Outras dicas

O uso acima de .retain () é necessária porque o statusItem está a ser destruída aquando do regresso do método applicationDidFinishLaunching (). Bind essa variável como um campo em casos de MyApplicationAppDelegate usando self.statusItem vez.

Aqui está um exemplo modificado que não requer um .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()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top