Sunday, December 5, 2010

PyGUI


I was thinking about doing some more GUI programming. A few years ago, I invented what I think is a terrific version of Sudoku, which uses colored squares rather than numbers, and I thought I might try to resurrect it.
So I've been looking at PyGUI this morning. It's a "cross-platform pythonic GUI API." The screenshots look nice (native OS X); it's been updated recently, and there's some active discussion, though not a lot. It requires PyObjC. As I mentioned (here), I got a frustrated about a year ago with PyObjC not working well with XCode, and haven't looked at it since. And so I forgot that it's actually included in the Apple System Python installed in Snow Leopard. And that python even has py2app!


cd ~/Desktop/PyGUI-2.3.1
sudo python setup.py install
python Demos/BlobEdit/blobedit.py
In any event, what I did first was to wander back to the PyObjC page at SourceForge and read:

It is technically possible to install new versions of PyObjC into the system install of python (such as using /usr/bin/easy_install).
We advise not to do that however, because bits of the system may use PyObjC
Hmmm. So I thought, maybe I'll use my MacPorts python for this. And I went to the easy_install docs to read up.

Multiple Python Versions ..
Also, if you are working with Python version 2.4 or higher, you can run Python with -m easy_install to run that particular Python version's
easy_install command.
My MacPorts python is here:

$ /opt/local/bin/python2.6
Python 2.6.6 (r266:84292, Nov 14 2010, 17:37:27)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

$ /opt/local/bin/python2.6 -m easy_install
complains about paths. According to this

Custom installation for a Mac OS X "User":
you should just use the ~/Library/Python/2.x/site-packages directory

because it's "set up to use .pth files", whatever that means. But, I decided to do something different, namely to use the site-packages directory of the MacPorts python:

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
I set up .pydistutils.cfg:

$ cat .pydistutils.cfg
[install]
install_lib = /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
install_scripts = ~/bin

sudo /opt/local/bin/python2.6 -m easy_install pyobjc==2.2
/opt/local/bin/python2.6
>>> import objc
>>> from Quartz import *
>>>
And this works too:

/opt/local/bin/python2.6 ~/Desktop/PyGUI-2.3.1/Demos/BlobEdit/blobedit.py
OK! I should've stopped there. But then I remembered py2app.

>>> import py2app
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named py2app
So I need to get that also.

sudo /opt/local/bin/python2.6 -m easy_install py2ap

cd ~/Desktop/PyGUI-2.3.1/Demos/BlobEdit
/opt/local/bin/python2.6 setup.py py2app -A
It complains about no such file:

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/py2app-0.5.2-py2.6.egg/py2app/apptemplate/prebuilt/main-x86_64
Looks like py2app might be out of date? I found a reference that said it's fixed and pointed me to here:

http://bitbucket.org/ronaldoussoren/py2app
And that I should do this:

$ hg clone http://bitbucket.org/ronaldoussoren/py2app
-bash: hg: command not found
hg is mercurial. Took a moment for the penny to drop.. And ran into another problem. To summarize: the easy_install version doesn't have main-x86_64, whatever that's about. The one that's fixed (Ronald Oussoren) fails because it's looking for modulegraph-0.8.1 when what I have is modulegraph-0.8-py2.6.egg, and that's what easy_install gives me when I ask it.
In the end, I removed Oussoren's version:

sudo rm -rf /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/py2app-0.5.3-py2.6.egg
reinstalled the easy_install version, and copy the file I need into it

sudo mv ~/Desktop/py2app/build/lib/py2app/apptemplate/prebuilt/main-x86_64 /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/py2app-0.5.2-py2.6.egg/py2app/apptemplate/prebuilt/main-x86_64
And now it works:

cd ~/Desktop/PyGUI-2.3.1/Demos/BlobEdit
/opt/local/bin/python2.6 setup.py py2app -A
open ~/Desktop/PyGUI-2.3.1/Demos/BlobEdit/dist/blobedit.app
Or just double-click on the icon. Whew!