Dynamic Content

When an input changes, it refreshes the refresh_widget passed to it upon construction. Pass an event as on_refresh to enable_refresh() in order to trigger code (such as recalculations).

Try it out

  • Change the A text input to 2 and press tab (notice the change to Sum)
  • Change the B dropdown (notice the change to Sum)


Sum: 5

class DynamicContentForm(Form):
    def __init__(self, view):
        super().__init__(view, 'my_cool_form')

        calculator = Calculator.for_current_session()

        grouped_inputs = self.add_child(FieldSet(view, legend_text='Calculator'))

        grouped_inputs.layout.add_input(TextInput(self, calculator.fields.operandA, refresh_widget=self)).use_layout(MarginLayout(2, right=True))
        grouped_inputs.layout.add_input(SelectInput(self, calculator.fields.operandB, refresh_widget=self))

        self.add_child(P(self.view, text='Sum: %s' % calculator.sum))