[Pyobjc-dev] Fwd: How to translate from Obj-C?
Brought to you by:
ronaldoussoren
|
From: Orestis M. <or...@or...> - 2008-09-23 17:26:55
|
Forwarding our conversation back to the list - don't forget to Reply to All next time, so people can search the archives! -- or...@or... http://orestis.gr/ Begin forwarded message: > From: Łukasz Dziedzic <dzi...@al...> > Date: 19 September 2008 19:23:47 BST > To: Orestis Markou <or...@or...> > Subject: Re: [Pyobjc-dev] How to translate from Obj-C? > > Thank you very much for help! > > Wil be back. Soon! > > Ł > > On Sep 19, 2008, at 1:16 AM, Orestis Markou wrote: > >> I think I've spotted something; sharedToolPaletteController is both >> an variable and a method. So while you set in the body of the class >> sharedToolPaletteController to be a class attribute, you then >> overwrite the name with a method. Changing it to something like >> that will work: >> >> class DFGlyphToolPaletteController(NSWindowController): >> >> _sharedToolPaletteController = None >> >> @classmethod >> def sharedToolPaletteController(cls): >> print "sharedToolPaletteController is", >> cls._sharedToolPaletteController >> if cls._sharedToolPaletteController is None: >> print "SHOUT IF HERE" #Never gets here! >> cls._sharedToolPaletteController = >> DFGlyphToolPaletteController.allocWithZone_(None).init() >> return cls._sharedToolPaletteController >> >> >> class DreamFontAppDelegate(NSObject): >> def showOrHideToolPalette_(self, sender): >> print "showOrHideToolPalette [" >> >> DFGlyphToolPaletteController >> .sharedToolPaletteController().showWindow_(None) >> print "] showOrHideToolPalette" >> >> >> Let me explain the error messages you got. >> >> sharedToolPaletteController is <selector >> sharedToolPaletteController of <objective-c class >> DFGlyphToolPaletteController at 0x1f06050>> >> >> This means that the name 'sharedToolPaletteController' in that >> scope is a "selector" (in python they are called methods) of that >> class. >> >> <type 'exceptions.AttributeError'>: 'objc.python_selector' object >> has no attribute 'showWindow_' >> >> This a standard error saying that the attribute you are trying to >> access (showWindow_) does not exist in that object. You could get >> the same error by trying to do: >> >> class MyClass(object): >> pass >> >> c = MyClass() >> print c.something #raises error >> >> Regarding your use of self, you are a bit confused :). Self is >> needed in class definitions, not in calls. Example: >> >> class MyClass(object): >> def a_method(self, argument): >> self.argument = argument >> print argument >> >> c = MyClass() >> c.a_method("hello world") # will print "hello world" >> print c.argument # likewise >> >> >> So you have to use self to attach and access attributes inside a >> class, but when you are using a class, you already have a name for >> it - in the previous example, "c" is that name. Inside a class >> definition, there is no such name, and this is why you use "self". >> Other languages (like C#) use something similar ("this", I think), >> but do not require it explicitly as Python does. It is one of these >> things that may be initially confusing, but very useful and clear >> in the long run. >> >> Cheers, >> Orestis >> -- >> or...@or... >> http://orestis.gr/ >> >> >> >> >> On 18 Sep 2008, at 23:23, Łukasz Dziedzic wrote: >> >>> Thanks a lot! >>> >>> On Sep 18, 2008, at 11:15 PM, Orestis Markou wrote: >>> >>>> I hope I'm not misleading you, but I think that since the method >>>> you're trying to translate is a "static" method, you have to >>>> change it to be a classmethod in python: >>>> >>>> class SKTToolPaletteController(object): >>>> sharedToolPaletteController = None #this is shared between all >>>> instances >>>> >>>> @classmethod # this allows you to call that on a class rather >>>> than the instance >>>> def sharedToolPaletteController(cls): >>>> if cls.sharedToolPaletteController is None: >>>> cls.sharedToolPaletteController = >>>> SKTToolPaletteController.allocWithZone_(None).init() >>>> return cls.sharedToolPaletteController >>>> >>>> Remember that in python there are no "global/static" attributes, >>>> so everything must be qualified. Hence the explicit "self" that >>>> points to the instance, and in the above code, the explicit >>>> "cls" (class is a keyword) that points to the class. >>>> >>>> I really suggest you learn a bit about Python, because you will >>>> always hit this kind of snags when trying to translate obj-c into >>>> it. There a few very good tutorials out there, pick one that you >>>> like and stick with it, I promise that you'll find Python very >>>> enjoyable. >>> >>> I FOUND Python enjoyable: I'm graphic designer, with no fluent >>> english and i wrote lots of macros in python, maybe not nice but >>> working. My problem is I don't understand some meanings, and i >>> have no programmers around to ask, so learning is bit painful. >>> Best way for me - by practice, step by step. Slowly, but works. >>> >>> >>> >>>> >>>> >>>> Hope that helped, >>> >>> I learned something! >>> but still doesn't work. This is what I have (names changed): >>> >>> class DFGlyphToolPaletteController(NSWindowController): >>> >>> sharedToolPaletteController = None >>> >>> @classmethod >>> def sharedToolPaletteController(cls): >>> print "sharedToolPaletteController is", >>> cls.sharedToolPaletteController >>> if cls.sharedToolPaletteController is None: >>> print "SHOUT IF HERE" #Never gets here! >>> cls.sharedToolPaletteController = >>> DFGlyphToolPaletteController.allocWithZone_(None).init() >>> return cls.sharedToolPaletteController >>> >>> >>> class DreamFontAppDelegate(NSObject): >>> def showOrHideToolPalette_(self, sender): >>> print "showOrHideToolPalette [" >>> >>> DFGlyphToolPaletteController >>> .sharedToolPaletteController().showWindow_(None) >>> print "] showOrHideToolPalette" >>> >>> causes error: >>> showOrHideToolPalette [ >>> sharedToolPaletteController is <selector >>> sharedToolPaletteController of <objective-c class >>> DFGlyphToolPaletteController at 0x1f06050>> >>> 2008-09-19 00:15:57.983 DreamFont[6700:10b] <type >>> 'exceptions.AttributeError'>: 'objc.python_selector' object has no >>> attribute 'showWindow_' >>> >>> because of selector I tried used .self... >>> >>> >>> DFGlyphToolPaletteController >>> .sharedToolPaletteController().self.showWindow_(None) >>> >>> Baaad.. >>> >>> 2008-09-19 00:20:10.814 DreamFont[6718:10b] <type >>> 'exceptions.TypeError'>: Expecting instance of NSWindowController >>> as self, got one of NoneType >>> >>> Where is a trick???? >>> >>> Ł >>> >>>> >>>> Orestis Markou >>>> >>>> -- >>>> or...@or... >>>> http://orestis.gr/ >>>> >>>> >>>> >>>> >>>> On 18 Sep 2008, at 22:05, Łukasz Dziedzic wrote: >>>> >>>>> Great! >>>>> >>>>> So I know now – first part in app delegate i translated well. >>>>> But I'm lost in SKTToolPaletteController. >>>>> >>>>> + (id)sharedToolPaletteController { >>>>> static SKTToolPaletteController *sharedToolPaletteController = >>>>> nil; >>>>> >>>>> if (!sharedToolPaletteController) { >>>>> sharedToolPaletteController = [[SKTToolPaletteController >>>>> allocWithZone:NULL] init]; >>>>> } >>>>> >>>>> return sharedToolPaletteController; >>>>> } >>>>> >>>>> My code: >>>>> >>>>> def sharedToolPaletteController(self): >>>>> #static? There is no static in Python, right? >>>>> sharedToolPaletteController = None >>>>> #next makes me no sense at all >>>>> if not sharedToolPaletteController: >>>>> sharedToolPaletteController = super(SKTToolPaletteController, >>>>> self).allocWithZone_(None).init(); >>>>> return sharedToolPaletteController >>>>> >>>>> But doesn't matter what inside def, could be just >>>>> print "whatever" >>>>> I have "error "<class 'objc.error'>: " always >>>>> >>>>> It must be some trick – when called >>>>> >>>>> SKTToolPaletteController >>>>> .alloc().sharedToolPaletteController().showOrHideWindow() >>>>> there where no error, but sense too. >>>>> >>>>> Completely lost, >>>>> >>>>> Ł >>>>> >>>>> On Sep 18, 2008, at 10:45 PM, Orestis Markou wrote: >>>>> >>>>>> #needed imports >>>>>> >>>>>> @IBaction >>>>>> def showOrHideToolPalette_(self, sender): >>>>>> SKTToolPaletteController >>>>>> .sharedToolPaletteController().showOrHideWindow() >>>>>> >>>>>> Don't forget that in python, every class method must have >>>>>> "self" as the first argument. Next time, please post your >>>>>> source code for whatever you've done, so we can look at that >>>>>> rather than trying to guess. >>>>>> >>>>>> Hope that helped, >>>>>> Orestis >>>>>> -- >>>>>> or...@or... >>>>>> http://orestis.gr/ >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> On 18 Sep 2008, at 19:34, Łukasz Dziedzic wrote: >>>>>> >>>>>>> Hi Everybody! >>>>>>> >>>>>>> I'm trying to do my own toolbox in IB. I'm watching carefully >>>>>>> Obj-C >>>>>>> example Sketch, and I found in app delegate: >>>>>>> >>>>>>>> - (IBAction)showOrHideToolPalette:(id)sender { >>>>>>>> >>>>>>>> // We always show the same tool palette panel. Its controller >>>>>>>> doesn't get deallocated when the user closes it. >>>>>>>> [[SKTToolPaletteController sharedToolPaletteController] >>>>>>>> showOrHideWindow]; >>>>>>>> >>>>>>>> } >>>>>>>> >>>>>>> >>>>>>> and in SKTToolPaletteController >>>>>>> >>>>>>>> + (id)sharedToolPaletteController { >>>>>>>> static SKTToolPaletteController *sharedToolPaletteController = >>>>>>>> nil; >>>>>>>> >>>>>>>> if (!sharedToolPaletteController) { >>>>>>>> sharedToolPaletteController = [[SKTToolPaletteController >>>>>>>> allocWithZone:NULL] init]; >>>>>>>> } >>>>>>>> >>>>>>>> return sharedToolPaletteController; >>>>>>>> } >>>>>>> >>>>>>> >>>>>>> All my attempts generates error "<class 'objc.error'>: need self >>>>>>> argument" :-( >>>>>>> >>>>>>> Could somebody help me? >>>>>>> >>>>>>> Ł >>>>>>> >>>>>>> >>>>>>> ------------------------------------------------------------------------- >>>>>>> This SF.Net email is sponsored by the Moblin Your Move >>>>>>> Developer's challenge >>>>>>> Build the coolest Linux based applications with Moblin SDK & >>>>>>> win great prizes >>>>>>> Grand prize is a trip for two to an Open Source event anywhere >>>>>>> in the world >>>>>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/ >>>>>>> _______________________________________________ >>>>>>> Pyobjc-dev mailing list >>>>>>> Pyo...@li... >>>>>>> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev >>>>>> >>>>> >>>> >>> >> > |