코코아와 파이썬 (pyobjc)으로 상태 표시 줄 항목을 어떻게 만들려면?
문제
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()
즉시 자동 정리되기 때문입니다.
다른 팁
ApplicationDidFinishLaunching () 메소드에서 수익을 내면 상태가 파괴되기 때문에 .retain ()의 위의 사용이 필요합니다. 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()
제휴하지 않습니다 StackOverflow