How to update a control in pyforms? ControlCombo

112 Views Asked by At

I have a Combo Box in which I need to update item values dynamically.

Currently using the following code:

def __init__(self, *args, **kwargs):
    super().__init__('Report')

    self._partNumber  = ControlText(label= 'Part Number')
    self._shellSize   = ControlCombo(label= 'Shell Size')

    self._partNumber.changed_event = self.__partNumberChanged

def __partNumberChanged(self):
    self._shellSize.clear()
    v = 25 if self._partNumber.value[-3:] == '805' else 50
    opts = [('5-13', 25), ('14', 40), ('15', v), ('16', 40), ('17', 50), ('18', 30), ('19', 50), ('21,23', 80)]

    for item in opts:
        self._shellSize += item

When combo items are printed every option shows up:

>> print(self._shellSize.items)
dict_items([('5-13', 25), ('14', 40), ('15', 25), ('16', 40), ('17', 50), ('18', 30), ('19', 50), ('21,23', 80)])

But form won't display all of them:

Form Control Image

Is there a way to refresh a control or update an item value? Any idea why this doesn't work properly?

Thanks!

1

There are 1 best solutions below

0
On

Pyforms won't allow duplicated values, I had to modify control_combo.py under pyforms_gui/controls/

add_item function

Change this function to allow duplicated values

   def add_item(self, label, value=ValueNotSet, allow_duplicated_values= False):
        self._addingItem = True
        if value is not ValueNotSet:
            if value not in self._items.values() or allow_duplicated_values:
                self._combo.addItem(label)
        else:
            if label not in self._items.keys():
                self._combo.addItem(label)

        firstValue = self._items == {}
        self._items[str(label)] = label if value is ValueNotSet else value
        
        self._addingItem = False

        if firstValue:
            self.value = self._items[label]

Now you can call it like this:

opts = [('5-13', 25), ('14', 40), ('15', v), ('16', 40), ('17', 50), ('18', 30), ('19', 50), ('21,23', 80)]

    for item in opts:
        self._shellSize.add_item(label='myLabel', value='myValue', allow_duplicated_values=True)