HelloWorld.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-core » Examples » Scripts » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc core » Examples » Scripts » HelloWorld.py
#!/usr/bin/env pythonw

# HelloWorld.py
#
# The original PyObjC interface example by Steve Majewski.
#

# 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 )

###
### NOTE:  This is no longer the recommended way to build applications
### using the pyobjc bridge under with OS X.  In particular, applications
### work much better if they are constructed in a proper app wrapper.
###
### This app does demonstrate that it is possible to build full
### featured Cocoa apps without InterfaceBuilder.
###

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

class AppDelegate (NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        print "Hello, World!"

    def sayHello_(self, sender):
        print "Hello again, World!"

def main():
    app = NSApplication.sharedApplication()

    # we must keep a reference to the delegate object ourselves,
    # NSApp.setDelegate_() doesn't retain it. A local variable is
    # enough here.
    delegate = AppDelegate.alloc().init()
    NSApp().setDelegate_(delegate)

    win = 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 = NSButton.alloc().initWithFrame_ (((10.0, 10.0), (80.0, 80.0)))
    win.contentView().addSubview_ (hel)
    hel.setBezelStyle_( 4 )
    hel.setTitle_( 'Hello!' )
    hel.setTarget_( app.delegate() )
    hel.setAction_( "sayHello:" )

    beep = NSSound.alloc()
    beep.initWithContentsOfFile_byReference_( '/System/Library/Sounds/Tink.Aiff', 1 )
    hel.setSound_( beep )

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

    adios = NSSound.alloc()
    adios.initWithContentsOfFile_byReference_(  '/System/Library/Sounds/Basso.aiff', 1 )
    bye.setSound_( adios )

    win.display()
    win.orderFrontRegardless()          ## but this one does

    AppHelper.runEventLoop()


if __name__ == '__main__' : main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.