Question

J'ai créé un tout nouveau projet dans XCode et j'ai les éléments suivants dans mon fichier 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)

Cependant, lorsque je lance l'application, aucun élément de la barre d'état ne s'affiche. Tous les autres codes dans main.py et main.m sont par défaut.

Était-ce utile?

La solution

Je devais le faire pour que cela fonctionne:

  1. Ouvrez MainMenu.xib. Assurez-vous que la classe du délégué de l'application est MyApplicationAppDelegate . Je ne sais pas si vous devrez le faire, mais c'est ce que j'ai fait. C'était faux et le délégué de l'application n'a jamais été appelé.

  2. Ajoutez statusItem.retain () car il est automatiquement auto-libéré. ??

Autres conseils

L'utilisation de .retain () ci-dessus est requise car statusItem est en cours de destruction au retour de la méthode applicationDidFinishLaunching (). Liez cette variable en tant que champ dans les instances de MyApplicationAppDelegate en utilisant self.statusItem.

Voici un exemple modifié ne nécessitant pas de fichier .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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top