Radio Buttons linked to a Submit button

Hello I am trying to link a series of Radio Buttons to a Submit button to then add value of the selected Radio Button to my Database.
Say I am working on a Survey and I have three options.
rb1 = RadioButton(text=“Happy”, group_name=‘radioGroup1’, value=“1”)
rb2 = RadioButton(text=“Neutral”, group_name=‘radioGroup1’, value=“2”)
rb3 = RadioButton(text=“Unhappy”, group_name=‘radioGroup1’, value=“3”)
Then I want to have a:
def submit_button_click(self, **event_args):
How do i add radio buttons to this section so then they get recorded when I click on the Submit button.
There are some options like get_group_value()
But I am not sure how to link everything together.
Thank you

I think you are on the right track here.

get_group_value() will return the value of the selected radio button in that group. As long as your values are unique, this will work. Otherwise you will have to test the selected property for each radio button.

If all your radio buttons are inside of a container (e.g., column panel, flow panel, link, etc…), then you can iterate through each radio button in that container. For example:

for radio in self.my_column_panel.get_components():

  if radio.selected:
     print(radio.text + ' is selected') # insert your logic here

Here is a clone to show you the iterative approach:
https://anvil.works/build#clone:I5VVVTSKT6PWHUUS=KWAYVWUCZ5ZY5JCLKFRS4RAC

On a side note, you can format your code on the forum by wrapping it in backticks with the word Python.

For example,

```python

print('this will be syntax highlighted')

```
2 Likes

Hello. Thank you for your reply.
Using your method I was able to get the radiobutton label radio.text and value radio.value.
I am still not able to actually record that radio.value in database. Any ideas on how I could add that value to database?
Thank you

Have you read the documentation on data tables? They will explain how to pass variables to your data table.

Feel free to show what you have tried so we can help with more specific details.

I figured it out. My mistake was that the output of radiobutton value is a string and I was trying to send it to numbers data. Sometimes it’s the silly mistakes.
Thank you for your help.

1 Like