Eto Checkbox from Layers Syntax


I have a question about syntax using rhinopython to create check boxes using Eto. I’ve been using Grasshopper for a while and have some programming experience with Java mostly and a little bit of python so this maybe a stupid question with a very easy answer but I cant seem to figure it out.

I would like to create a checkbox for each layer in the file. Normally, I would do it like this

# Create controls for the dialog
self.m_label = forms.Label(Text = 'Layers:')
if layers:
for i in range (0,len(layers)):
    self.m_checkbox[i] = forms.CheckBox (Text = layers[i]) 

This is the error I get
Message: 'UserInterface' object has no attribute 'm_checkbox'

So for now, I’m doing it manually like this but it seems like a stupid way of doing it since different files might have different number of layers.

# Create controls for the dialog
self.m_label = forms.Label(Text = 'Layers:')
if layers:
for i in range (0,len(layers)):
    self.m_checkbox = forms.CheckBox (Text = layers[0])
    self.m_checkbox2 = forms.CheckBox (Text = layers[1])
    self.m_checkbox3 = forms.CheckBox (Text = layers[2])

Can anyone point me the correct way of doing this? Thanks

How about using a list?

if layers:
  self.m_checkboxes = [forms.CheckBox (Text = layer) for layer in layers]

Thanks for the quick reply Graham. Do you know how I would then add them to the layout and check their states?

No sorry I haven’t used ETO. (I am still on Rhino5 ).

You can then iterate over them with

for checkbox in self.m_checkboxes:
  # do something

Thank you Graham, that seems to have done the trick. Only thing is, again, Im sure this is a very stupid question but how can I keep track of the the index in the loop, I need to do something like

    for checkbox in self.m_checkboxes:

and more importantly when I’m checking their state

def GetState(self):
    if self.m_checkbox.Checked : layersToProcess.append(layers[0])
    if self.m_checkbox2.Checked : layersToProcess.append(layers[1])

as I said, I think I’m doing things the JAVA / Processing way which isnt ideal for Python

No problem, you can use enumerate to get the index as well as the item, eg

for i, checkbox in enumerate(checkboxes):
  # do something with checkbox and checkboxes[i+1]

Thanks Graham, looks like its all coming together now

1 Like