PyGTK lists made easy with Kiwi

Making nice lists is an utter pain with PyGTK. GTK+ does you the pleasure of separating the model and the view. I guess it is a reasonable idea in theory (don't they always teach you to separate the model and the view?) and allows you to have alternative views of the same model, and allows you to extend the implementation in any sort of way. BUT, to repeat: in most cases it is an utter pain. Even the simplest list becomes a mess of boilerplate code.

Anyone who has done a decent amount of PyGTK programming will have written their own implementation of model+view mixed together, but the best one I have used is from Kiwi. If you have never used it, Kiwi is a framework that lessens some of the painful aspects of PyGTK development. In fact, it's so useful, let's call it a library.

Creating a pythonic view of a list of objects is easy. Let's have a look at some code:


from kiwi.ui.objectlist import ObjectList, Column

class Fruit(object):
def __init__(self, name, is_red):
self.name = name
self.is_red = is_red


fruits = ObjectList(
[
Column('name', title='Fruit Name'),
Column('is_red', title='Is the fruit red?',
data_type=bool, radio=True)
]
)

fruits.append(Fruit('Apple', True))
fruits.append(Fruit('Melon', False))


Yes it is really that easy. Add a bit of code to put it in a window:


import gtk
w = gtk.Window()
w.add(fruits)
w.show_all()
gtk.main()


And you are ready to go. If you try to achieve the same thing with pure PyGTK, you will write at least 10 times as much code.

Popular posts from this blog

PyGTK, Py2exe, and Inno setup for single-file Windows installers

ESP-IDF for Arduino Users, Tutorials Part 2: Delay

How to install IronPython2 with Mono on Ubuntu