python PyObjc系统Icon.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python PyObjc系统Icon.py相关的知识,希望对你有一定的参考价值。

import objc, re, os
from Foundation import *
from AppKit import *
from PyObjCTools import NibClassBuilder, AppHelper

# poach one of the iSync internal images to get things rolling
status_images = {'idle':'/Users/tehnix/Desktop/dropboxstatus-pause-lep.png'}

start_time = NSDate.date()

class Timer(NSObject):
    images = {}
    statusbar = None
    state = 'idle'

    def applicationDidFinishLaunching_(self, notification):
        statusbar = NSStatusBar.systemStatusBar()
        # Create the statusbar item
        self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
        # Load all images
        for i in status_images.keys():
        self.images[i] = NSImage.alloc().initByReferencingFile_(status_images[i])
        # Set initial image
        self.statusitem.setImage_(self.images['idle'])
        # Let it highlight upon clicking
        self.statusitem.setHighlightMode_(1)
        # Set a tooltip
        self.statusitem.setToolTip_('Sync Trigger')
        
        # Build a very simple menu
        self.menu = NSMenu.alloc().init()
        # Sync event is bound to sync_ method
        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Sync...', 'sync:', '')
        self.menu.addItem_(menuitem)
        # Default event
        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
        self.menu.addItem_(menuitem)
        # Bind it to the status item
        self.statusitem.setMenu_(self.menu)
        
        # 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 = Timer.alloc().init()
    app.setDelegate_(delegate)
    AppHelper.runEventLoop()

以上是关于python PyObjc系统Icon.py的主要内容,如果未能解决你的问题,请参考以下文章

python pyobjc的例子。

python macOS PyObjc Computername

python pyobjc:获取finder的可见窗口路径

python 在OS X上强制使用pyobjc自动发现时区

python 使用PyObjC和NSWorkspace设置桌面图片。我是个伪君子!

使用 Esky 冻结/打包 Cocoa PyObjC Python 应用程序