pyobjc-0.6 released for macosx

Steven D. Majewski sdm7g at Virginia.EDU
Fri Apr 6 16:47:15 EDT 2001


There's finally a release of pyobjc for macosx on 
	<http://sourceforge.net/projects/pyobjc/>


Source distribution should build against the current cvs of
python2.1beta or the next release candidate.

If you have the last beta distribution tarball, you need to add
".m" to the src_extensions assignment in distutils/unixccompiler.py:

change:
    src_extensions = [".c",".C",".cc",".cxx",".cpp" ]
to:
    src_extensions = [".c",".C",".cc",".cxx",".cpp",".m"]


There is also a binary .so library -- If you have Python2.1
installed, you can just copy it into the python lib's 
site-packages directory. 



There are still some bugs, known and unknown, notably:

 Delegates don't seem to work -- which is very limiting in AppKit!

 You need to use the whole pathname of python when launching a 
     script (or else use DropShell or gdb) or you may get a 
    'Error 1011 in _sendFinishLaunchingNotification'
    (This is probably an OSX bug.)

 There are some classes exposed in the pyobjc runtime that you 
     really don't need and you really shouldn't try to access. 
     If you do, you will crash. The classes are NSProxy and it's
     subclasses. These will be removed from visibility in the
     runtime eventually. 

  NSWindow makeKeyAndOrderFront: doesn't  seem to work from 
     Python. No idea why. I'm sure there are others I haven't found. 


We need to get delegates working, and figure out how to load and
animate Nib files from Python for pyobjc to be practical for 
writing a real Cocoa app in Python. 

In the mean time, here is a working sample using Cocoa AppKit. 

-- Steve Majewski




# HelloWorld.py
#
# You have to run this script from the command line with 
# the full pathname for python:
#    /usr/local/bin/python HelloWorld.py 
#
# or else run from DropShell or gdb. Anything else and you will get a:
# 	Error 1011 in _sendFinishLaunchingNotification 
# and it wont work.
#
# -- Steve Majewski <sdm7g at Virginia.EDU>
#

# You can look up these classes and methods in the Cocoa docs.
# A quick guide to runtime name mangling:
#
#      ObjC 		becomes 	  Python
#    [ obj method ]   			obj.method()
#    [ obj method: arg ]  		obj.method_(arg)
#    [ obj method: arg1 withOtherArgs: arg2 ] 
#				obj.method_withOtherArgs_( arg1, arg2 )

import pyobjc
rt = pyobjc.runtime	# shorthand -- runtime gets used a lot!

def main():

    pool = rt.NSAutoreleasePool()

    # Load Application Framework:
    rt.NSBundle.bundleWithPath_( 
	'/System/Library/Frameworks/AppKit.framework').load()

    NSApp = rt.NSApplication.sharedApplication()

    win = rt.NSWindow.alloc()
    frame = ((200.0, 300.0), (250.0, 100.0))
    win.initWithContentRect_styleMask_backing_defer_ (frame, 15, 2, 0)
    win.setTitle_ ('HelloWorld')
    win.setLevel_ (3)			# floating window

    hel = rt.NSButton.alloc().initWithFrame_ (((10.0, 10.0), (80.0, 80.0)))
    win.contentView().addSubview_ (hel)
    hel.setBezelStyle_( 4 )
    hel.setTitle_( 'Hello!' )

    beep = rt.NSSound.alloc()
    beep.initWithContentsOfFile_byReference_( 
	'/System/Library/Sounds/tink.aiff', 1 )
    hel.setSound_( beep )

    bye = rt.NSButton.alloc().initWithFrame_ (((100.0, 10.0), (80.0, 80.0)))
    win.contentView().addSubview_ (bye)
    bye.setBezelStyle_( 4 )
    bye.setTarget_ (NSApp)
    bye.setAction_ ('stop:')
    bye.setEnabled_ ( 1 )
    bye.setTitle_( 'Goobye!' )

    adios = rt.NSSound.alloc()
    adios.initWithContentsOfFile_byReference_( 
	'/System/Library/Sounds/Basso.aiff', 1 )
    bye.setSound_( adios )
    
    win.display()
#    win.makeKeyAndOrderFront_ (NSApp)	## This doesn't seem to  work 
    win.orderFrontRegardless()		## but this one does

    NSApp.run()    


if __name__ == '__main__' : main()







More information about the Python-list mailing list