[Pyobjc-dev] Problem with PyObjc-1.1b2 and key value binding
Brought to you by:
ronaldoussoren
From: <mat...@da...> - 2004-05-03 09:54:54
|
Greetings, This is my first post to this group, possibly because only recently I've touched the bleeding edge of PyObjc on Panther ;-). Otherwise, I'm greatly impressed by the bridge and its huge (magically working) functionality. It means I can now continue my NEXTSTEP 3.3 style programming (yes, yes I still have an Intel box running), yet under much better conditions. Though Objective-C is not that bad for GUI programming (at least it is much better suited than C++), Python offers even more pleasures. First of all, many thanks especially to Bill, Bob, Jack and Ronald, to name only the four prominent names that come up to my mind. Sorry that I can't mention all the many others explicitly. Thank you all! Here comes my problem: Last week I dipped my nose a little into Apple's key-kalue binding (KVB) paradigm offered on Panther. Though documentation is scarce by now, it appears to me that this is indeed a major advance compared to the AppKit of the good old NEXTSTEP days. I mean advance in the sense of "simplifying your own code and reducing the time to create it". So I played around with KVB using PyObjc 1.0. Unfortunetely, it did not really work well (I ran into segmentation faults) , so I gave PyObjc-1.1b2 a try. The nasty segmentation faults went away and all seemed to work as expected, but now I've got problems with access to Objective-C variables (outlets in particular). The normal way, e.g. 'obj.outlet', does no longer work. Apparently as soon as some controller watches 'obj'. However, I can use PyObjCTools.KeyValueCoding.getKey to obtain access. Here are instructions to create a barebones example that exhibit the problem: Create a new Cocoa-Python Application called 'Test' in Project Builder Open Main.nib in Interface Builder Drag an object controller instance from the palette to the applicationn's nib file. Connect the contoller's 'content' outlet to the application's delegate. Add key 'buttonName' to the controller keys. Drag a button to the application's window. Select 'Bindings' in the inspector. Unfold parameters 'title'. Select 'buttonName' from the model key path pulldown menu. Edit the class 'TestApplicationDelegate' in Interface Builder by adding an outlet 'myOutlet'. Connect the outlet 'myOutlet' of the TestApplicationDelegate instance to the button in the main window. Edit the file TestAppDelegate.py to contain the following: from Foundation import NSLog from PyObjCTools import NibClassBuilder from PyObjCTools.KeyValueCoding import getKey NibClassBuilder.extractClasses("MainMenu") class TestAppDelegate(NibClassBuilder.AutoBaseClass): def applicationDidFinishLaunching_(self, aNotification): print "usual attribute access" try: print "myOutlet:", self.myOutlet except Exception, what: print print what print "access using getKey" print "myOutlet:", getKey(self, 'myOutlet') NSLog( "Application did finish launching." ) def buttonName(self): return 'Hello' Save the nib file, build and run the application The button shows the name 'Hello', thanks to key/value binding'. However, I can't access outlets in the usual way anymore. I get the following output in the console: usual attribute access myOutlet: 'NSNotifying_PyKVBAppDelegate' object has no attribute 'myOutlet' access using getKey myOutlet: <NSButton: 0x3d23a0> 2004-05-03 09:32:56.488 PyKVB[4196] Application did finish launching. In order to demonstrate that this unexpected behavior has to to with KVB, I can uncheck the 'Bind' feature for the button's title. After I saved the nib file and restarted the application, I now get: usual attribute access myOutlet: <NSButton: 0x3d1ff0> access using getKey myOutlet: <NSButton: 0x3d1ff0> 2004-05-03 09:53:14.638 PyKVB[4202] Application did finish launching. Of course, the name of the button is no longer 'Hello', because key value observing is now inactive. I've somewhere encounterd the term 'isa-swizzling'. Maybe the problem has to do with that. Regards, Matthias -- Matthias Oberlaender DaimlerChrysler AG Research and Technology Information and Communication RIC/AP (Machine Perception) P.O. Box 2360 D-89013 Ulm |