[Pyobjc-dev] Calling python from objc
Brought to you by:
ronaldoussoren
From: Akhar <ak...@gm...> - 2004-04-20 23:40:17
|
I seem to have a bit of a problem with calling python objects from cocoa apps. I did find your post on soureforge about how it could be done (http://sourceforge.net/mailarchive/message.php?msg_id=7503787). I tried to do as the poster did however I need to call the python code from another object that is linked to a custom view. Here is what I have : -----main.m #import <Cocoa/Cocoa.h> #import <PythonGlue.h> #import <Script.h> int main(int argc, const char *argv[]) { return NSApplicationMain(argc, argv); } -----myoutput.h, /* outputView */ #import <Cocoa/Cocoa.h> #import <PythonGlue.h> #import <Script.h> @interface outputView : NSView { NSString *command; NSColor *bgColor; NSMutableDictionary *attributes; Script *script; } - (void)prepareAttributes; - (void)drawStringCenteredIn: (NSRect)bounds; - (void)setBgColor:(NSColor *)c; - (NSColor *)bgColor; - (void)setString: (NSString *)cmd; - (NSString *)command; @end ----myoutput.m, #import "outputView.h" @implementation outputView - (id)initWithFrame:(NSRect)rect { if (self = [super initWithFrame:rect]) { [[PythonGlue alloc] init]; scr = [Script instantiateSubclassNamed:@"pyScript"]; // this is where it should run the python code NSLog(@"initializing view the big one"); [self prepareAttributes]; [self setBgColor:[NSColor yellowColor]]; [self setString:@"Nothing to see here! move on!"]; } return self; } ... more ... but ... cut ... @end ----Script.h, /* Shell */ #import <Cocoa/Cocoa.h> @interface Script : NSObject + instatiateSubclassNamed: (NSString *) aClassName; @end @interface Script (pyScript) - (void) test; @end ----Script.m, #import "Script.h" @implementation Script(pyScript) + instantiateSubclassNamed:(NSString*) aClassName { Class aClass = NSClassFromString(aClassName); return [[aClass alloc] init]; } @end ----pyScript.py from objc import * from Foundation import * from AppKit import * Script = lookUpClass("Scipt") class pyScript(Script): def init(self): print "creating object" return self def test_(self, obj): print "in python here test worked" and the pythonglue.h/.m and the python framework. From that standpoint all seems great. I have the exact same setup as he does except for the main.m that was left as is. instead in myoutput.h I created the Script *py; variable to my knowledge this is not the problem, I get a: objc.nosuchclass_error: 'Script' is not defined. error. What am i doing wrong? Regards Stephane |