Not stupid at all - that's about how I would have done it too, except for
the style and arguments handling.

I don't typically add a base style parameter to __init__ since (1) you can
put a default style in a CSS sheet for your new widget class, (2) the
built-in Settables class that widgets inherit from already provides an
automatic way to call setStyle at __init__ time, and (3) you probably will
want additional methods for controlling styles anyway. The reason for (3)
is that most of the complex *Panel classes are implemented as HTML tables,
so sometimes that causes unexpected results when styles are applied to the
table. For example, some styles applied to a table are not inherited to
text children in the cells on some browsers. Therefore, you might have to
apply it directly to the children as part of __init__, or (more commonly)
provide a setStyleXXX method to apply a style to a specific child.

SO - if you want users of this control to be able to set the style of the
label and value, you might offer:

  def setLabelStyle(self, label_style="my_label_style")
  def setValueStyle(self, value_style="my_value_style")

More importantly, when building a widget, it's best to use the dynamic args
support. For example, look at the source code for the HorizontalPanel class
you are using. It (and almost every other widget) uses the '**kwargs'
pattern for handling optional args. With all the above in mind, you could
have written (hope I did this bug-free):


class LabelValue(HorizontalPanel):

    def __init__(self, label, value, **kwargs):
        kwargs['StyleName'] = kwargs.get('StyleName',
"my-default-LabelValue-style")
        HorizontalPanel.__init__(self, HorizontalAlignment = HasAlignment.
ALIGN_LEFT, VerticalAlignment = HasAlignment.ALIGN_MIDDLE, **kwargs)
        self.my_label = HTML(label, StyleName="my_label_style")
        self.my_value = HTML(value, StyleName="my_value_style")
        self.add(self.my_label)
        self.add(self.my_value)

    def setLabelStyleName(self, label_style):
        self.my_label.setStyleName(label_style)

    def setValueStyleName(self, value_style):
        self.my_value.setStyleName(value_style)

    def update(self, value):
        self.value.setText(value)

With the above implementation, you can put default styles in your style
sheet for the widget (my-default-LabelValue-style) as well as the label (
my_label_style) and the value (my_value_style). Now, users of your widget
have options for styles like this:

# just use LV default styles
name_LV = LabelValue("Name","fill-in here")

# or set our own
age_LV = LabelValue("Age","47", StyleName="age-style",
LabelStyleName="age-label-style", ValueStyleName="age-value-style")

Or at any time with calls like this:

age_LV.setLabelStyleName("some-style-name")


Hope this is helpful,
Rich

On Thu, Jun 28, 2012 at 8:47 PM, Daniel Gonzalez <gonva...@gmail.com> wrote:

> I ended up defining this as shown below:
>
> class LabelValue(HorizontalPanel):
>
>
>     def __init__(self, label, value, label_style = 'label', value_style =
> 'value', own_style = None):
>         HorizontalPanel.__init__(self, HorizontalAlignment = HasAlignment.
> ALIGN_LEFT, VerticalAlignment = HasAlignment.ALIGN_MIDDLE)
>         label = MyHTML(label, label_style)
>         self.value = MyHTML(value, value_style)
>         self.add(label)
>         self.add(self.value)
>         if own_style: self.setStyleName(own_style)
>
>
>     def update(self, value):
>         self.value.setText(value)
>
>
> The idea being that I want a different style for the label and for the
> value, and that I want to change the value now and then.
> For the time being it is serving my purposes, but this is my first
> "personal widget", so please let me know if I am doing something terribly
> stupid.
>
> Thanks,
> Daniel
>
> On Tuesday, June 19, 2012 2:29:01 PM UTC+2, Jim Washington wrote:
>>
>> On Mon, 2012-06-18 at 14:44 -0700, Daniel Gonzalez wrote:
>> > Hi,
>> >
>> >
>> > I am trying to define my first simple widget, but I am not
>> > suceeding.What I am trying to define is a widget for a Label/Value
>> > pair. Label and Value must have configurable style names, so that text
>> > size, color, etc can be defined independently in the CSS. And they
>> > must be horizontally next to each other, with label placed before
>> > value.
>> >
>> >
>> > I have tried several implementation without success. It is not clear
>> > for me from which base class I should derive my widget. Directly from
>> > Widget? My first idea was to use two different DIVs, one for label and
>> > one for value, but I do not know how to add two DOM elements to a
>> > widget.
>>
>> The base widget for a custom widget is usually ComplexPanel. It has the
>> methods and infrastructure for managing (adding and removing) child DOM
>> elements. You still have to use DOM methods, but despite the name, it
>> removes some of the complexities. Grep through the ui folder for
>> examples of how ComplexPanel is used.
>>
>> - Jim Washington
>>
>>
>>
>>
>>
>>

Reply via email to