Monday, May 5, 2008

def __init__(self):


Never thought I'd see the day when I start a blog (today). What I hope to do is provide hints and code that will help people using Python, particularly for exploring Bioinformatics. Since I am an unabashed Mac 'fanboy' a substantial part of what I put here will be Mac and Cocoa-specific, but certainly not all.

Here is a link to my .Mac pages. I show a bunch of simple examples using Cocoa and PyObjC. Something about .Mac, they don't allow Google to index the site, and these pages were never really visible to the web. It has been a while since I wrote much of that stuff, so parts may be broken.

PyObjC is wonderful, but I found it confusing to start with because the documentation is limited and much of it outdated. Also, simple examples tend not to be simple enough, in my experience. Yesterday I started playing with PyObjC again, and learned that things are a bit different now with XCode 3.0 and IB 3.0. In the old days, you would add an outlet to your classes in IB and then just call an appropriate method for that outlet in your code. Now, there is an extra step needed, a call to objc.IBOutlet() as shown in the code below. Actions setup in IB are also supposed to be decorated, although my first test showed that this was not actually required.

from Foundation import *
from AppKit import *
import MyView, MyDataLoader

class PyTreeAppDelegate(NSObject):
    myView = objc.IBOutlet()
    myDataLoader = objc.IBOutlet()

    def applicationDidFinishLaunching_(self, sender):
        NSLog("Application did finish launching.")

    def awakeFromNib(self):
        self.myDataLoader.loadExample()
        self.myView.setNeedsDisplay_(True)

    @objc.IBAction()
    def buttonPushed_(self,sender):
        doStuff()


Link to the code.

Update: I forgot to mention, you only get PyObjC (and of course, XCode and Interface Builder) if you install the Developer tools from the Leopard install disk.

No comments: