views:

43

answers:

3

I am writing a small diagram drawing application (similar to Graphviz in spirit), and need a GUI library that would allow me to embed a canvas capable of drawing anti-aliased lines and text. I want to have a text editor in one half of the window to edit the diagram code and a (perhaps live) preview pane in the other.

Right now I have the text editor in a tkinter window and the rendered diagram in a separate pygame one. This technically works, but it's messy (e.g. having two event loops), and in general I would much prefer having both parts in one window. I have searched for ways of integrating the two, but haven't been able to find anything cross-platform, and pygame explicitly suggests not trying to do it.

An alternative would be to have pygame export the image into a file and load it back into tkinter, but tkinter can read only GIF/PPM without PIL (and I use Python 3, which PIL doesn't support) and pygame can't write GIF/PPM. I could backport to Python 2, since it's a tiny app, but even then, having a large extra library for a simple image conversion doesn't seem right, and the round-trip to a file will probably be too slow for live preview (not to mention ugly).

Finally, a simple tkinter canvas is almost what I want, except it can't draw anti-aliased lines, and for a program whose main purpose is to draw line figures, that is not acceptable.

I'm using Python 3 so libraries that support it are preferred, but if there's no way to do that whatsoever Python 2 libs are Ok as well. The library needs to be cross-platform, and of course, the fewer external packages are required, the better.

+1  A: 

If you don't mind the way GTK looks, pygtk has an option for antialising in their canvas widget (see this) and is considered by many to be as powerful as Tkinter, though it is not included in standard Python installs.

Also, it's Python 3.x compatible, which can't be said of most non-standard library modules and packages.

Rafe Kettler
Are there Windows binaries for the Python 3 version, or at least a Py3-compatible source distribution? 20 minutes of searching the PyGTK site and repositories produced no results for either of those.
Max Shawabkeh
No, it doesn't seem that any Windows binaries exist. I'm sorry I ever recommended pygtk, I wasn't aware the Windows support was this poor (it's very much in vogue and easily installed on Linux and other unix-likes). There is a version that is Py3k compatible, but it's rather hard to get, it seems.
Rafe Kettler
It seems like you might be best off sticking with Tkinter and using an extension like PIL (or maybe if you're lucky you can find something that works for Python 3.x). Sadly, such is life in a Python universe where the more modern release is rarely used because no one takes the time to write packages for it.
Rafe Kettler
A: 

Screwing around with Tkinter+pygame is silly. I would use wxPython. In fact, I've done a diagramming widget using wxPython, and it has anti-aliasing:

alt text

Unfortunately it was for work, so I can't distribute the code.

The wxPython classes you want to look at for anti-aliasing are wx.GCDC and/or wx.GraphicsContext.

FogleBird
Unfortunately, wxPython is not compatible with Python 3. It is a nice fallback, but for now I still haven't lost hope of getting something that works with Python 3. In fact, one of my main goals for this project was to evaluate how viable Python 3 is.
Max Shawabkeh
Lots of 3rd party libraries aren't compatible with Python 3 yet -- that should be part of your "evaluation."
FogleBird
+1  A: 

After a thorough search I ended up using PyQt4. It does fit all my requirements (Python 3, cross-platform, anti-aliasing), and now that I've gotten through the basics, it's also quite intuitive and easy to use.

Posting this as an answer to my own question and accepting it for future reference.

Max Shawabkeh