Good day!
I guess I found a strange behaviour when calling sizer.Replace(container_widget, widget_rendered_inside_the_container_widget) that doesn’t calls widget_rendered_inside_the_container_widget.SetContainingSizer(sizer)… I need to call it manually (look at mine clear_validation function)
So here is an example of how I’m going to use it. Maybe this become interesting for somebody?
coding=utf-8
import wx
import formencode
import wx.lib as wxlib
from formencode import validators
class Form(formencode.Schema):
allow_extra_field=True
filter_extra_fields=True
summary = validators.UnicodeString(not_empty=True)
description = validators.UnicodeString(not_empty=True)
priority = validators.OneOf([u’major’])
type = validators.OneOf([u’Дефект’, u’Задача’, u’Улучшение’])
component = validators.OneOf([u’trunk’])
cc = validators.Email(not_empty=True)
class MainPanel(wx.Panel):
def init(self, parent=None, id=-1, **kwargs):
wx.Panel.init(self, parent=parent, id=id, **kwargs)
self.construct_widgets()
self.layout_widgets_with_labels()
self.validation_widgets = list()
self.validation_containers = list()
self.wrong_widgets = list()
self.wrong_widgets_parent = list()
def construct_widgets(self):
self.summary = wx.TextCtrl(parent=self, id=-1)
self.type_choices = [u’Дефект’, u’Улучшение’, u’Задача’]
self.type = wx.Choice(parent=self, id=-1, choices=self.type_choices)
self.description = wx.TextCtrl(parent=self, id=-1, size=(500,400), style=wx.TE_MULTILINE|wx.TE_BESTWRAP|wx.TE_AUTO_SCROLL|wx.TE_AUTO_URL)
self.cc = wx.TextCtrl(parent=self, id=-1)
self.submit_button = wx.Button(parent=self, id=-1, label=u’Сохранить’)
self.submit_button.Bind(wx.EVT_BUTTON, self.do_submit)
def layout_widgets_with_labels(self):
main_label = wx.StaticText(parent=self, id=-1, label=u’Создание задачи в Trac’, style=wx.ALIGN_CENTER)
main_label.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.NORMAL))
summary_label = wx.StaticText(parent=self, id=-1, label=u’Краткое описание’)
type_label = wx.StaticText(parent=self, id=-1, label=u’Тип задачи’)
description_label = wx.StaticText(parent=self, id=-1, label=u’Полное описание’, style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)
cc_label = wx.StaticText(parent=self, id=-1, label=u’Ваш адрес электронной почты’)
main_box = wx.GridBagSizer()
main_box.Add(main_label, pos=wx.GBPosition(row=0, col=0), span=wx.GBSpan(colspan=2), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(cc_label,pos=wx.GBPosition(row=1, col=0), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(self.cc,pos=wx.GBPosition(row=1, col=1), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(type_label, pos=wx.GBPosition(row=2, col=0), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(self.type, pos=wx.GBPosition(row=2, col=1), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(summary_label, pos=wx.GBPosition(row=3, col=0), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(self.summary,pos=wx.GBPosition(row=3, col=1), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(description_label, pos=wx.GBPosition(row=4, col=0), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(self.description, pos=wx.GBPosition(row=4, col=1), span=wx.GBSpan(), flag=wx.EXPAND|wx.ALL, border=10)
main_box.Add(self.submit_button, pos=wx.GBPosition(row=5, col=0), span=wx.GBSpan(colspan=2), flag=wx.EXPAND|wx.ALL, border=10)
self.SetSizer(main_box)
self.Layout()
def do_submit(self, evt):
print 'in the do_submit'
data = dict()
data[‘type’] = self.type_choices[self.type.GetCurrentSelection()]
data[‘summary’] = self.summary.GetValue()
data[‘description’] = self.description.GetValue()
data['component'] = 'trunk'
data[‘cc’] = self.cc.GetValue()
data[‘priority’] = ‘major’
data = self.validate(data)
if not data:
pass
def clear_validation(self):
i = 0
while i < len(self.validation_widgets) :
validation_widget = self.validation_widgets[i]
validation_widget.GetContainingSizer().Detach(validation_widget)
wrong_widget = self.wrong_widgets[i]
wrong_widget.Reparent(self)
container_widget = self.validation_containers[i]
sizer = container_widget.GetContainingSizer()
sizer.Replace(container_widget, wrong_widget)
container_widget.Destroy()
wrong_widget.SetContainingSizer(sizer)
sizer.Layout()
i = i + 1
self.validation_widgets = list()
self.validation_containers = list()
self.wrong_widgets = list()
self.wrong_widgets_parent = list()
def validate(self, data):
self.clear_validation()
schema = Form()
valid_data = None
try :
valid_data = schema.to_python(data)
except formencode.Invalid, error:
errors = error.error_dict
for k, v in errors.items():
widget_to_go = getattr(self, k)
base_sizer = widget_to_go.GetContainingSizer()
sizer_item = base_sizer.GetItem(widget_to_go)
panel = wx.Panel(parent=self, id=-1)
base_sizer.Replace(widget_to_go, panel)
self.wrong_widgets_parent.append(widget_to_go.GetParent())
widget_to_go.Reparent(panel)
message_text = wx.StaticText(parent=panel, id=-1, label=v.message)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(widget_to_go, proportion=sizer_item.GetProportion(), flag=sizer_item.GetFlag())
sizer.Add(message_text)
panel.SetSizer(sizer)
panel.Layout()
self.Layout()
self.GetParent().Layout()
self.GetParent().Fit()
self.validation_containers.append(panel)
self.validation_widgets.append(message_text)
self.wrong_widgets.append(widget_to_go)
return False
return valid_data
class MainForm(wx.Frame):
def init(self, parent=None, id=-1, **kwargs):
wx.Frame.init(self, parent=parent, id=id, style=wx.CLOSE_BOX)
self.panel = MainPanel(parent=self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.panel, flag=wx.ALL|wx.EXPAND, border=10)
self.SetSizerAndFit(sizer)
···
28 окт. 2008, в 00:52, C M написал(а):
Please make a small runnable sample and post it to the list.
And can you state what is the effect you want to achieve, because I am
having trouble understanding what the purpose of the replacing and
such is. It seems like it just may be unnecessarily complex.
Che
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Илья Дёшин
ЗАО СК УНИКА-Жизнь
ilya@uniqa.kiev.ua
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users