Can a databinding (or setattr) be used to set radiobuttons when a value can be boolean or none?

I have a form with several groups of buttons using a uniform scheme (group1_yes, group1_no) that I would like to set when the form loads. Since I don’t want to set the buttons to a value if none has been selected (None in the table), I can’t figure out how to do this through a data binding - ie using self.item[‘Group1val’) and not self.item(‘Group1val’) as this would select the “no” button if the value is none.

Hence, I tried using setattr for all the buttons:

def set_btn(self, btn, val):
    if val is None:
        pass
    else:
        setattr(self, btn+'_yes.selected', val)
        setattr(self, btn+'_no.selected', not val)

I since discovered that setattr appears to be disabled for form components. Thus, I would like to know if there is an approach that I have missed to setting these values?

Hi Henrik!

You could use self.item['groupname'] == False, which distinguishes from None.

However, what property of the button are you trying to set? Your code is trying to replace the Button object with a boolean, which Anvil is preventing you from doing because it’s not what you want to do here!

I guess you’ll want to switch the background color, perhaps? Maybe bind the background property to something like '#800' if self.item['groupname'] == False else '#ccc', which will turn the button blue if that group is set to False.

Thank you, that solved the problem! Regarding the code, I had it right in my app - setattr(self, btn+’_yes.selected, val), but missed in when I typed it in the question.

Hi @henrik

Just friendly suggestion: you can make your code easier to read by formatting it nicely on the forum. For example:

```python

print('this will be syntax highlighted')

```

Thanks, I thought I did so using the button in the editor, but will make sure it works the next time. I fixed the code above as well.

1 Like