pythonpython-2.7urwid

urwid changing a display attribute


I am creating a configurator that lets users choose from a set of legal values. For each legal value I create a Choice (which is essentially a MenuButton). If the user selects a Choice, then the choice changes the value to its caption.

Notice that the Choice with the current value has a different attribute than the others. I'd like to loop through the choices after a pick is made and change the attributes to match the new value.

I can loop through the choices by passing the legal_choices list to all the Choices (or do something more clever) but I don't know how to write code that loops through a list of widgets and changes their attributes.

            for vv in leg_values:
                choice = Choice(vv, self, item)
                if vv == self.get_var(item):
                    choice = urwid.AttrMap(choice,'cur_value')
                legal_choices.append(choice)
            var_item = SubMenu(top,item,legal_choices)

Does anyone know how to change attributes on a widget in urwid?


Solution

  • I had the same problem with buttons but 3 years later I still could not find an answer. This worked for me after experimenting:

    button = urwid.Button("some label")
    urwid.connect_signal(button, 'click', self.item_chosen, task)
    # Assigning original AttrMap 
    button = urwid.AttrMap(button, None, focus_map='reversed')
    
    # At a later point you can change the AttrMap used like this and the button color will change:
    button.attr_map = {None: "active task"}