[Pythonmac-SIG] Simple Statusitem example

Dethe Elza delza at livingcode.org
Wed Apr 13 08:17:55 CEST 2005


Here is a very simple example of a statusbar item, requiring no further  
resources.  A real statusitem would probably have a Nib to load a menu  
and maybe a configuration panel from, icons, etc.

================ begin statusitem.py ======================

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

start_time = NSDate.date()


class Timer(NSObject):
     '''
     Application delegate
     '''
     statusbar = None

     def applicationDidFinishLaunching_(self, notification):
         print 'timer launched'
         # Make the statusbar item
         statusbar = NSStatusBar.systemStatusBar()
         # if you use an icon, the length can be NSSquareStatusItemLength
         statusitem =  
statusbar.statusItemWithLength_(NSVariableStatusItemLength)
         self.statusitem = statusitem  # Need to retain this for later
         # statusitem.setImage_(some_image)
         #statusitem.setMenu_(some_menu)
         statusitem.setToolTip_('Seconds since startup')
         statusitem.setAction_('terminate:') # must have some way to exit
         self.timer =  
NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repea 
ts_(
             start_time,
             1.0,
             self,
             'display:',
             None,
             True
         )
         NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer,  
NSDefaultRunLoopMode)
         self.timer.fire()

     def display_(self, notification):
         print 'display:'
         self.statusitem.setTitle_(elapsed())


def elapsed():
     return str(int(NSDate.date().timeIntervalSinceDate_(start_time)))

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

====================== end statusitem.py  
================================

The main thing about the setup file is to pass LSUIElement = '1' in the  
plist to suppress the dock icon.
The program should perhaps hide itself as well.

====================== begin setup.py ==================================

'''
Minimal setup.py example, run with:
% python setup.py py2app
'''

from distutils.core import setup
import py2app

NAME = 'Uptime'
SCRIPT = 'statusitem.py'
VERSION = '0.1'
ID = 'uptime'

plist = dict(
     CFBundleName                = NAME,
     CFBundleShortVersionString  = ' '.join([NAME, VERSION]),
     CFBundleGetInfoString       = NAME,
     CFBundleExecutable          = NAME,
     CFBundleIdentifier          = 'org.livingcode.examples.%s' % ID,
     LSUIElement                 = '1'
)


app_data = dict(script=SCRIPT, plist=plist)

setup(
   app = [app_data],
)

====================== end setup.py ====================================

--Dethe


Windows has detected the mouse has moved. Please restart your system  
for changes to take effect.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2488 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20050412/ea18e28b/smime.bin


More information about the Pythonmac-SIG mailing list