I'm planning to do a screencast for this, but knowing myself, I'll just put the code here in case I run off to a different direction tomorrow...

So, I present a lightly annotated hello world app in PyObjC! Only works on Mac OS X 10.5.2.

Download PyObjC tutorial (24 KB)


  • One IBAction
  • One IBOutlet
  • A QTMovieView, a label and a button
  • Cocoa Bindings to Python attributes
  • Notifications and selectors

When you roll your own, here are some tips:

  • Don't forget that IBOutlets must be class level
  • Do init(self), not __init__(self)
  • Always initialize things you bind to (None will do), or you will get nasty obscure errors
  • if you have other Pythons installed (eg. Macports), do: alias macpython= /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python and use that. Also remove other Pythons installed in /Library/Frameworks or Xcode might get confused.
  • If the app isn't launching, do macpython main.py and check for syntax errors (you can't launch it)
  • If you don't get the expected behaviour, check the console for swallowed errors.
May 17, 2008, 12:07 a.m. More (175 words) 7 comments Feed
Previous entry: Whither PyObjC?
Next entry: Explosion: Aftermath



Comment by vchahun , 8 years, 8 months ago :

I collected a list of bookmarks about PyObjc here: http://vstock.free.fr/pyobjc.html
If you have any suggestion concerning it, since you seem to be actively interested by PyObjc, please email me. I hope this could be useful to those who want to learn PyObjc, even though most of the tutorials are quite old ...


Comment by vchahun , 8 years, 8 months ago :

And by the way ... I forgot to say that your example is interesting: it is well written, and might help those who try to fight against XCode 2 to understand how to write correct PyObjc code ...


Comment by Orestis Markou , 8 years, 8 months ago :

That's a great list! Do you mind if I send an email to pyobjc-dev about this? It'll greatly help beginners.


Comment by vchahun , 8 years, 8 months ago :

Of course, you can.


Comment by PyObjCNewbie , 8 years, 7 months ago :

Here is a very simple tutorial that I wrote yesterday, before I found this page. It's meant for real beginners like myself, since I had a hard time to figure out the most basic things in Xcode 3 (most tutorials assume older Xcode versions). Please have a look, and feel free to copy and improve. I'd especially hear your comments about the yellow boxes - these are things I (as a Newbie) noticed.


Comment by Orestis Markou , 8 years, 7 months ago :

Thanks for the feedback! Some of my observations after reading your tutorial:

It's very important to read up on the architecture of Cocoa before trying to create even small applications with it. It's very different from most of the GUI frameworks out there, so trying to do things in the same way you're used to will only lead to frustration.

To communicate with the GUI, Cocoa has the notion of "Outlets" and "Actions":

Actions are invoked after user interaction (a click of a button or the editing of a cell). In your tutorial, the "work_" method you defined is an Action. There is a decorator to define actions: @objc.IBAction. If you annotate methods with that, they will appear in IB without having to redo them.

For the sake of completeness, I'll mention here outlets, defined with "var = objc.IBOutlet()". These will appear in IB3 as outlets, allowing you to make connections, without having to retype everything.

NB: For the above magic to work, XCode must be running, and your project must be open.

The method that you describe about the GUI synchronizing itself to the contents of a variable is called "Bindings". You bind a property of a GUI element to the value of a variable. Unfortunately, this only works for Objective-C variables and not for Python variables, because Objective-C objects fire notifications behind the scenes to inform the GUI that they have changed. This is called Key-Value Observing (KVO). You can do it with Python, but you have to fire the notifications yourself, which can become tedious.

Hope that helped!


Comment by PyObjCNewbie , 8 years, 7 months ago :

Thanks, that helped a lot! Someone (you?) should write an introductory book that doesn't assume that one wants to learn C and ObjC just in order to program Cocoa using PyObjC.

This post is older than 30 days and comments have been turned off.