Python – IBoutlet with PyObjC and Interface Builder

cocoainterface-builderpyobjcpython

I'm writing a simple OSX app using Python and PyObjC. I designed the settings dialog using Interface Builder and I use ibtool to compile it, then load it from Python. The problem is how to access the controls I have in this window from the Python code? I played around with iPhone development a bit before and I remember I need to have an IBOutlet in the controller class which will be connected to the UI control in the interface builder. It should look something like this in Python:

class MyClass(NSObject):
  my_outlet = objc.IBOutlet('my_outlet')

But since I'm not working in XCode (all I have is a .py file and a .xib file), Interface Builder doesn't know about my outlets. How can I do the binding in this case? Or how else can I access the UI elements from the code?

Best Answer

First, the use of Xcode or not has nothing to do with NIB loading (beyond making it more convenient).

As Ole said, you can use IB to manually add the outlet's you need to file's owner or to the custom object instances that you have in the NIB file. By doing so, it will all "just work".

However, this statement is what prompted my relatively similar answer:

all I have is a .py file and a .xib file

Are you trying to write a bit of UI code outside of a .app wrapper? If so, that is a wholly unsupported pattern, very difficult to get correct, and quite likely to break across software updates or major releases (as it has many times in the past).

The best way to solve your problem is to use an Xcode project and build a standard application. The templates are no longer shipped with the dev tools. Just download them separately.

If you need to run it from the command line, you can still do so.