[PEAK] Trellis-ified wx Dialog

Phillip J. Eby pje at telecommunity.com
Thu Jul 19 14:55:55 EDT 2007


At 02:43 PM 7/19/2007 -0400, Phillip J. Eby wrote:
>     class EditBridge(trellis.Component):
>         cell = None
>         widget = None
>
>         def __init__(self, **kw):
>             super(EditBridge, self).__init__(**kw)
>             self.widget.Bind(wx.EVT_KILL_FOCUS, self.write)
>
>         @trellis.rule
>         def read(self):
>             self.widget.SetValue(self.cell.value)
>
>         def write(self, event):
>             self.cell.value = self.widget.GetValue()

By the way, another way to do the above would be:

     class EditBridge(trellis.Component):
         trellis.values(
             widget = None,
             cell = None,
         )
         trellis.rules(
             setup = lambda self: self.widget.Bind(wx.EVT_KILL_FOCUS, 
self.write),
             read = lambda self: self.widget.SetValue(self.cell),
         )
         def write(self, event):
             self.cell = self.widget.GetValue()

One of the interesting effects of doing it this way, is that you can 
change an EditBridge's "widget" and it'll attach itself to the new 
widget.  Well, it'll keep receiving events from the previous widget 
without some way of unbinding, but it's an amusing anyway.  :)

By the way, notice the differnce between the two versions: the first 
version doesn't make 'cell' a cell; it's just an ordinary 
attribute.  So the first version has to set and get the '.value' of the cell.

The second version makes 'cell' a cell, so it can simply be read and 
written directly.

Both versions, however, can be passed an actual Cell object for the 
'cell' keyword.  The latter version can also operate without being 
given a cell, or it can just be passed an actual *value* to 
display/edit.  You could then have other rules referencing the 
EditBridge's .cell attribute, in order to do other things with it.




More information about the PEAK mailing list