dynamic adding of the widget problem

Good day!

I’m writting some application and decided to utilize formencode validation for it.

And I’ve got a strange problem, when I try to layout the errors:

	base_sizer = widget_to_go.GetContainingSizer()

panel = wx.Panel(parent=self, id=-1)

res = base_sizer.Replace(widget_to_go, panel)

message_text = wx.StaticText(parent=panel, id=-1, label=v.message)

sizer = wx.BoxSizer(wx.VERTICAL)

sizer.Add(widget_to_go)

sizer.Add(message_text)

panel.SetSizer(sizer)

base_sizer.Layout()

I can replace only the widget, or the sizer, or the sizerItem (replacing the SizerItem doesn’t helped me, 'cause I don’t understand how to get the oldindex parameter for calling Replace(size_t oldindex, wx.SizerItem) )

In this case mine widget_to_go have the parent - the form panel. And then I want to place it on the different panel. As I can see from code and from the debug - everything placed correctly. But the widget_to_go after such manipulations goes to the top left corner of the form panel.

Can I solve this ?? Can you help me with getting the oldindex of the SizerItem. or laying out correctly the widgets

Best regards,

Ilya Dyoshin

Try replacing

base_sizer.Layout()

with

panel.Layout()

Che

···

On Mon, Oct 27, 2008 at 4:42 PM, Ilya Dyoshin <ilya@uniqa.kiev.ua> wrote:

Good day!
I'm writting some application and decided to utilize formencode validation
for it.
And I've got a strange problem, when I try to layout the errors:
base_sizer = widget_to_go.GetContainingSizer()

                panel = wx.Panel(parent=self, id=-1)
                res = base_sizer.Replace(widget_to_go, panel)

                message_text = wx.StaticText(parent=panel, id=-1,
label=v.message)
                sizer = wx.BoxSizer(wx.VERTICAL)
                sizer.Add(widget_to_go)
                sizer.Add(message_text)
                panel.SetSizer(sizer)
                base_sizer.Layout()
I can replace only the widget, or the sizer, or the sizerItem (replacing the
SizerItem doesn't helped me, 'cause I don't understand how to get the
oldindex parameter for calling Replace(size_t oldindex, wx.SizerItem) )
In this case mine widget_to_go have the parent - the form panel. And then
I want to place it on the different panel. As I can see from code and from
the debug - everything placed correctly. But the widget_to_go after such
manipulations goes to the top left corner of the form panel.
Can I solve this ?? Can you help me with getting the oldindex of the
SizerItem. or laying out correctly the widgets
Best regards,
Ilya Dyoshin

That doesn't helped! the widget's continue to show in the top left corner.

28 окт. 2008, в 00:09, C M написал(а):

···

Try replacing

base_sizer.Layout()

with

panel.Layout()

Che
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

That doesn't helped! the widget's continue to show in the top left corner.

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

···

On Mon, Oct 27, 2008 at 6:21 PM, Ilya Dyoshin <ilya@uniqa.kiev.ua> wrote:

28 окт. 2008, в 00:09, C M написал(а):

Try replacing

base_sizer.Layout()

with

panel.Layout()

Che
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Good day

now I’ve made it working!!!

	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()

The main difference is that I’m caling Reparent on the widget

I need this for validating complex form using formencode. And when form validated and it has errors I’m showing the message of the error down to the widget in which data inputed.

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

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

minor bugfix:

in the clear_validation chnage the line

from

wrong_widget.Reparent(self)

to

wrong_widget.Reparent(self.wrong_widgets_parent[i])

and that would be completely correct even with complex layouts!

28 окт. 2008, в 01:30, Ilya Dyoshin написал(а):

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


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Илья Дёшин

ЗАО СК УНИКА-Жизнь

ilya@uniqa.kiev.ua