Skip to main content

ChoiceGroup

Radio buttons let people select a single option from two or more choices.

Examples​

Live demo

Basic ChoiceGroup​

import pglet
from pglet import ChoiceGroup, choicegroup, Button, Text
with pglet.page("basic-choicegroup") as page:
def button_clicked(e):
t.value = f"ChoiceGroup value is: {cg.value}"
page.update()

t = Text()
b = Button(text='Submit', on_click=button_clicked)
cg = ChoiceGroup(label='Select color', options=[
choicegroup.Option('Red'),
choicegroup.Option('Green'),
choicegroup.Option('Blue')])

page.add(cg, b, t)
input()

ChoiceGroup with icons​

import pglet
from pglet import ChoiceGroup, choicegroup
with pglet.page("choicegroup-with-icons") as page:
page.add(ChoiceGroup(label='Pick one icon', options=[
choicegroup.Option(key='day', text='Day', icon='CalendarDay'),
choicegroup.Option(key='week', text='Week', icon='CalendarWeek'),
choicegroup.Option(key='month', text='Month', icon='Calendar')
]))

ChoiceGroup with change event​

import pglet
from pglet import ChoiceGroup, choicegroup, Text
with pglet.page("choicegroup-with-change-event") as page:

def choicegroup_changed(e):
t.value = f"ChoiceGroup value changed to {cg.value}"
t.update()

cg = ChoiceGroup(label='Select color', on_change=choicegroup_changed, options=[
choicegroup.Option('Red'),
choicegroup.Option('Green'),
choicegroup.Option('Blue')
])

t = Text()

page.add(cg, t)

input()

Change items in ChoiceGroup options​

import pglet
from pglet import ChoiceGroup, choicegroup, Textbox, Button, Stack
with pglet.page("change-choicegroup-options") as page:

def find_option(option_name):
for option in cg.options:
if option.key == option_name:
return option
return None

def add_clicked(e):
cg.options.append(choicegroup.Option(option_textbox.value))
option_textbox.value = ''
page.update()

def delete_clicked(e):
option = find_option(cg.value)
if option !=None:
cg.options.remove(option)
page.update()

cg = ChoiceGroup()
option_textbox = Textbox(placeholder='Enter new item name')

add = Button("Add", on_click=add_clicked)
delete = Button("Delete selected", on_click=delete_clicked)
stack = Stack(controls = [cg, Stack(horizontal=True, controls=[option_textbox, add, delete])])

page.add(stack)

input()

Properties​

NameTypeDefaultDescription
valuestringkey value of the selected option.
labelstringDescriptive label for the choice group.
focusedboolfalseWhen set to true the focus is set on the control when it's shown on the page or page opened.

Events​

NameDescription
changeFires when the choice has been changed.
focusFires when the control has received focus.
blurFires when the control has lost focus.

Child controls​

Option control​

Option represents an item within ChoiceGroup.

NameTypeDefaultDescription
keystringOption's key. text value will be used instead if key is not specified.
textstringOption's display text. key value will be used instead if text is not specified.
iconstringIcon name to display with this option.
iconColorstringIcon color.