On 13 Jun, 2007, at 8:03, Kevin Walzer wrote:
I'm looking at the possibility of porting a Tkinter-based application toCocoa, and since my app is already written in Python, PyObjC is the logical starting point for me. However, what I'm not sure about is howmuch translation I'd have to do between Python datatypes and Objective-Cdatatypes. For instance, the code below reads data as a list, massages the data a bit, then returns a tuple that is then inserted into a Tkinter table display. if self.catname == 'All': self.catlist = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK) else: self.catlist = os.popen('/sw/bin/fink list --section=%s' % self.catname, 'r', os.O_NONBLOCK) for line in self.catlist: newline = line.split('\t') rawcat = newline[0] if rawcat == '(i)': firstcat=rawcat.replace('(i)', 'outdated') elif rawcat == ' i ': firstcat=rawcat.replace('i', 'current') elif rawcat == ' p ': firstcat=rawcat.replace('p', 'provided') else: firstcat = rawcat newlist = (firstcat, newline[1], newline[2], newline[3].strip('\n')) #finalline) self.infotable.insert('end' , newlist) Except for the line "self.infotable.insert('end' , newlist)", there is nothing Tkinter-specific in this code. If I used Interface Builder for the table view, would this data importcleanly into the GUI, or would I have to set up some sort of NS- prefixeddatatype (NSMutableArray?) to parse and display the data correctly? If so, how could this snippet be rewritten?
There is a tablemodel example included with PyObjC.Python lists, tuples and dictionaries are proxied using a subclass of the corresponding NS* type. You almost never have to use the NS* types explictly.
The major exception to this is Key-Value Observing (also known as Cocoa bindings). Cocoa bindings requires that objects emit events when they are mutated (append an item, set the value for a key in a dict, ...). Sadly enough Python doesn't have the hooks that are required to emit these events for pure python objects. Adding this support requires some low-level changes to python and I'm not sure if this can be done without adversely affecting the performance of the interpreter.
My preference in porting my application is to refactor the GUI layer from Tkinter to PyObjC, but otherwise retain as much of my code as possible in its present (Python-generic) form. Is this a feasibleapproach, or does using PyObjC require adopting more of an "Objective-C"mindset, designing the entire app from the top down to use NS*-datatypes? That's a lot more work and I might as well learn Objective-C and just use that.
Learning a little Objective-C might not be a bad idea anyway, you can find a lot of examples and code snippets on the web and most of those are written in ObjC. The major reasons for not switching to ObjC are the normal reasons for liking Python over compiled languages and the enormous amount of libraries available in Python. Something like Twisted beats all available networking libraries for Cocoa with a huge margin and simular advantages are available for other use cases.
Ronald
TIA, Kevin -- Kevin Walzer Code by Kevin http://www.codebykevin.com _______________________________________________ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig