Trying to set a checkbox value to checked if True

I am trying to set a checkbox to checked if the value of a variable is True

Here is a clone to represent my problem
https://anvil.works/build#clone:4MM24VHJ6H2OBKHK=33OMUSA4BELRTOFHZSND23LX
I want to set the checkbox to checked if the selection is True.

Here is the code:
    value = self.value.selected_value
    if value==True:
      print(f"Your Choice was: {value}")  
      self.checkbox_assigned.checked
    else:
      print(f"Your Choice was : {value}") 
     
    pass

The print statements, provide the correct True or False
Help. What am I missing???

Thanks for the clone.
It’s usually best to combine the clone with a code snippet.
Especially if the code snippet isn’t particularly long.

(Following the template when you start a new topic will help)

1 Like

It is really simple. I’ll just provide a clue here that the checked property of checkbox accepts boolean values (True/False).

The Docs on Checkbox might also help you in your case

Edit: I checked the app and realized that your problem was something else. So here is another hint for you - the values in the Dropdown items are strings, not booleans.

Thanks Stu, I am getting there!

Thanks, so should I do True==1, False==0

I am not sure what you will achieve by that. However, I will like to point out that eval() function can be used for getting the boolean value from the string.

Otherwise, you can “tell” your program that the selected value is going to be a string and not boolean.

2 Likes

you’re right that didn’t work, because True still activates the checkbox value. Its how I am writing

self.checkbox_assigned.checked

Has your problem been solved now?

1 Like

no :slight_smile:

would it be better to actually create the component, rather than try to populate an existing UI element?, then if condition is true I can create a pre-selected box, like this?

MyCheckBox([checked=True, text="Trip is Assigned])

I have tried various of combining the ([checked=True]) with
self.checkbox_assigned.checked
all combinations I have tried have resulted in bad syntax errrors.

The reason for the errors is that ‘[’ is not required here. Try without them and it will work

MyCheckBox(checked=True, text="Trip is Assigned")

There are multiple examples of such codes in the Docs and Tutorials so please consider checking them out.

1 Like

I’m on a cell now, I can’t clone. But if I understand you need to assign a value to something.

I see in the snippet a line with self.checkbox_assigned.checked. This line is not executing a function nor is assigning a value to something.

Translating from python to English:
self means “a form”.
self.checkbox_assigned means “a checkbox”.
self.checkbox_assigned.checked means “the status of the checkbox”. If you tell that to your app, the app does nothing.

I you instead tell your app “please set the status of that check box to checked”, then the app knows what to do. That translates to python self.checkbox_assigned.checked = True

2 Likes

Hi Stefano, thank you for the reply. I did try that, but it wasnt checking the tick on the check box, I just tried it again exactly as you said and its not working.

I am going to try to create the component from code and see if that works, TBH its probably a better way to do it.

This is happening because you are passing a string value to it. You need to use eval() to convert string into boolean

1 Like

Thank you, I decided to go with your original answer and add components dynamically, it works.

Thank you once again

1 Like