[PEAK] Multi-Component init failure (wxTemperature dialog example)
Phillip J. Eby
pje at telecommunity.com
Tue Sep 4 18:29:49 EDT 2007
At 02:50 PM 9/4/2007 -0700, Grant Baillie wrote:
>I realized that my prior "Trellis-ified wx Dialog" example is back to
>having issues the issues I saw here:
>
>http://www.eby-sarna.com/pipermail/peak/2007-July/002744.html
>
>The code was posted on the list, and can be found in the archives here:
>
>-------------- next part --------------
>A non-text attachment was scrubbed...
>Name: wxTemperature.py
>Type: text/x-python-script
>Size: 2225 bytes
>Desc: not available
>Url : http://www.eby-sarna.com/pipermail/peak/attachments/
>20070719/7c76e02c/wxTemperature.bin
>
>
>Basically, I can get around this with some None checking, but IIRC it
>was working in a prior svn release.
Here's the version I've been using, that still works:
from peak.events import trellis
import wx
import wx.xrc
#----------------- domain ----------------------
class TempConverter(trellis.Component):
trellis.values(
F = 32,
C = 0,
)
trellis.rules(
F = lambda self: self.C * 1.8 + 32,
C = lambda self: (self.F - 32)/1.8,
)
# ------------- ---------------
class EditBridge(trellis.Component):
trellis.values(
widget = None,
cell = None,
)
trellis.rules(
setup = lambda self: wx.EVT_KILL_FOCUS(self.widget, self.write),
read = lambda self: self.widget.SetValue(unicode(self.cell)),
)
def write(self, event):
self.cell = float(self.widget.GetValue())
#----------------- app ----------------------
class ConverterDialog(trellis.Component):
trellis.rules(
fField = lambda self: EditBridge(
widget = wx.xrc.XRCCTRL(self.dialog, "ID_F_FIELD"),
cell = self.converter.__cells__['F']
),
cField = lambda self: EditBridge(
widget = wx.xrc.XRCCTRL(self.dialog, "ID_C_FIELD"),
cell = self.converter.__cells__['C']
),
summary = lambda self: wx.xrc.XRCCTRL(self.dialog, "ID_SUMMARY"),
converter = lambda self: TempConverter(),
dialog = lambda self: TemperatureDialog(parent=None, id=-1)
)
@trellis.rule
def updateSummary(self):
if self.converter.F > 80.0:
self.summary.SetLabel(u"Phew!")
elif self.converter.C < 5.0:
self.summary.SetLabel(u"Brrr!")
else:
self.summary.SetLabel(u"")
class TemperatureDialog(wx.Dialog):
def __init__(self, parent, *args, **kw):
super(TemperatureDialog, self).__init__(parent, *args, **kw)
resources = wx.xrc.XmlResource("wxTemperature.xrc")
pre = wx.PreDialog()
resources.LoadOnDialog(pre, parent, "TemperatureDialog")
self.PostCreate(pre)
quit = wx.xrc.XRCCTRL(self, "ID_QUIT")
self.Bind(wx.EVT_BUTTON, lambda event: wx.GetApp().Exit(), quit)
if __name__ == "__main__":
app = wx.PySimpleApp(redirect=False)
cd = ConverterDialog()
cd.dialog.Show()
app.MainLoop()
More information about the PEAK
mailing list