How do I create a radio button group?

I’m trying to create a group of three radio buttons, the first of which is “selected” by default and the other two are false, but I can’t figure out how to group them together. Currently, the first of the three is active, but when I click on the others they just activate too (and stay that way) rather than behaving as a group.

I’ve tried:

  • Giving each button the same id
  • Adding a group="radioGroup" to each
  • Passing the same onclick function to each

The strangeness continues…

This doesn’t work (using label to mark the radio buttons):

local dlg = Dialog("No Dice")
    :separator{ text="Apply to frames" }
    :radio{ id="applyAll", label="All", selected=true, onclick=selectFrameRange }

    :newrow()
    :radio{ id="applyRange", label="Range", onclick=selectFrameRange }
    :number{ id="firstFrame", text="1", decimals=0 }
    :label{ text="to" }
    :number{ id="lastFrame", text=tostring(numFrames), decimals=0 }

    :newrow()
    :radio{ id="applySelected", label="Selected", onclick=selectFrameRange }

    :separator()
    :button{ id="ok", text="OK" }
    :button{ id="cancel", text="Cancel" }
    :show()

but this does (using text to mark them instead):

local dlg = Dialog("This works")
    :separator{ text="Apply to frames" }
    :radio{ id="applyAll", text="All", selected=true, onclick=selectFrameRange }

    :newrow()
    :radio{ id="applyRange", text="Range", onclick=selectFrameRange }
    :number{ id="firstFrame", text="1", decimals=0 }
    :label{ text="to" }
    :number{ id="lastFrame", text=tostring(numFrames), decimals=0 }

    :newrow()
    :radio{ id="applySelected", text="Selected", onclick=selectFrameRange }

    :separator()
    :button{ id="ok", text="OK" }
    :button{ id="cancel", text="Cancel" }
    :show()

Apparently using label at all breaks the radio group completely, even if they all have the same label. If anyone can explain this behavior I’d appreciate it. I feel like I’m almost certainly missing something.

Hi @sudo_whoami, the first radio that has a label creates a new group, use the text property to set the text of the specific radio option.

E.g.

local dlg = Dialog("Radio Groups")
 :radio{ label="Group1", text="A" }:newrow()
 :radio{                 text="B" }:newrow()
 :radio{                 text="C" }
 :radio{ label="Group2", text="A" }
 :radio{                 text="B" }
 :radio{                 text="C" }
 :separator()
 :button{ id="ok", text="OK" }
 :button{ id="cancel", text="Cancel" }
 :show()

image

1 Like

@dacap Ah, that makes sense. Thanks for the clarification!

2 Likes