This Blog continues on http://aliafshar.github.io/blog

Sunday, November 05, 2006

Zope in a gui, fields as widgets

This is a long quest, and at this stage we have objects, which implement interfaces, and we have schemas that reflect the interfaces, and are helpers for creating views for the objects. This is all well and good, but what happens on a widget level in a gui?

Well we can give you a simple example. The view is an adaption of the Schema, and which then has its content set (see the last blog about this topic). The view can be built by adapting each of the schemas fields into a gui element, exactly as you would build a web form.

Say you have an attribute defined as a zope.schema.Text, and you wanted an editor field. Well simply, using pygtk:

import gtk
from zope.interface import implements, Interface, Attribute
from zope.component import adapts, provideAdapter
from zope.schema import Text

class IWidget(Interface):
def write(value):
"""display the value"""

class TextWidget(gtk.Entry):
adapts(Text)
implements(IWidget)

def __init__(self, field):
self._field = field
super(TextWidget, self).__init__()

def write(self, value):
self.set_text(value)

provideAdapter(TextWidget)

Now everything is set up to adapt the field to a widget.

name = Text(title=u'The name of something')
print IWidget(name)

Gives you

<TextWidget object (GtkEntry) at 0xb715f16c>

Yay! Now you have everything you need to create a gui from a zope object.