1#!/usr/bin/env pythonw 2 3# HelloWorld.py 4# 5# The original PyObjC interface example by Steve Majewski. 6# 7 8# A quick guide to runtime name mangling: 9# 10# ObjC becomes Python 11# [ obj method ] obj.method() 12# [ obj method: arg ] obj.method_(arg) 13# [ obj method: arg1 withOtherArgs: arg2 ] 14# obj.method_withOtherArgs_( arg1, arg2 ) 15 16### 17### NOTE: This is no longer the recommended way to build applications 18### using the pyobjc bridge under with OS X. In particular, applications 19### work much better if they are constructed in a proper app wrapper. 20### 21### This app does demonstrate that it is possible to build full 22### featured Cocoa apps without InterfaceBuilder. 23### 24 25import objc 26from Foundation import * 27from AppKit import * 28from PyObjCTools import AppHelper 29 30class AppDelegate (NSObject): 31 def applicationDidFinishLaunching_(self, aNotification): 32 print "Hello, World!" 33 34 def sayHello_(self, sender): 35 print "Hello again, World!" 36 37def main(): 38 app = NSApplication.sharedApplication() 39 40 # we must keep a reference to the delegate object ourselves, 41 # NSApp.setDelegate_() doesn't retain it. A local variable is 42 # enough here. 43 delegate = AppDelegate.alloc().init() 44 NSApp().setDelegate_(delegate) 45 46 win = NSWindow.alloc() 47 frame = ((200.0, 300.0), (250.0, 100.0)) 48 win.initWithContentRect_styleMask_backing_defer_ (frame, 15, 2, 0) 49 win.setTitle_ ('HelloWorld') 50 win.setLevel_ (3) # floating window 51 52 hel = NSButton.alloc().initWithFrame_ (((10.0, 10.0), (80.0, 80.0))) 53 win.contentView().addSubview_ (hel) 54 hel.setBezelStyle_( 4 ) 55 hel.setTitle_( 'Hello!' ) 56 hel.setTarget_( app.delegate() ) 57 hel.setAction_( "sayHello:" ) 58 59 beep = NSSound.alloc() 60 beep.initWithContentsOfFile_byReference_( '/System/Library/Sounds/Tink.Aiff', 1 ) 61 hel.setSound_( beep ) 62 63 bye = NSButton.alloc().initWithFrame_ (((100.0, 10.0), (80.0, 80.0))) 64 win.contentView().addSubview_ (bye) 65 bye.setBezelStyle_( 4 ) 66 bye.setTarget_ (app) 67 bye.setAction_ ('stop:') 68 bye.setEnabled_ ( 1 ) 69 bye.setTitle_( 'Goodbye!' ) 70 71 adios = NSSound.alloc() 72 adios.initWithContentsOfFile_byReference_( '/System/Library/Sounds/Basso.aiff', 1 ) 73 bye.setSound_( adios ) 74 75 win.display() 76 win.orderFrontRegardless() ## but this one does 77 78 AppHelper.runEventLoop() 79 80 81if __name__ == '__main__' : main() 82