Hi all
I have created an ETO form with radio buttons.
I don’t know how to use the selected value in my list.
The code below creates a simple form with a two-choice radio button, “Option 1” and “Option 2”.
When I click on the “OK” button, I want to print the value of radio button selection or do something with it. I have tried, but I can’t.
I’m not an expert.
#! python3
# Imports
import Rhino
import scriptcontext
import System
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
# Sample Eto Radio Button class
class SampleEtoRadioButtonDialog(forms.Dialog[bool]):
# Dialog box Class initializer
def __init__(self):
super().__init__()
# Initialize dialog box===================================Window Title
self.Title = 'Test Radio Button'
self.Padding = drawing.Padding(10)
self.Resizable = True
#===========================================================Buttons
# Create the default button
self.DefaultButton = forms.Button()
self.DefaultButton.Text ='OK'
self.DefaultButton.Click += self.OnOKButtonClick
#====================================================Create Radio Button
self.m_radiobuttonlist = forms.RadioButtonList()
self.m_radiobuttonlist.DataStore = ['Option 1', 'Option 2']
self.m_radiobuttonlist.Orientation = forms.Orientation.Vertical
self.m_radiobuttonlist.SelectedIndex = 0
self.m_radiobuttonlist_ExpGroup=forms.RadioButtonList()
# Create controls for the dialog==============Create Radio Button Label
self.m_labelProfile = forms.Label()
self.m_labelProfile.Text = 'Select an Option:'
self.m_textboxProfile = forms.TextBox()
self.m_textboxProfile.Text = ""
# Create the abort button
self.AbortButton = forms.Button()
self.AbortButton.Text ='Cancel'
self.AbortButton.Click += self.OnCloseButtonClick
# Create a table layout and add all the controls
layout = forms.DynamicLayout()
layout.Spacing = drawing.Size(30, 10)
layout.AddRow(self.m_labelProfile, self.m_radiobuttonlist)
layout.AddRow(self.DefaultButton, self.AbortButton)
# Set the dialog content
self.Content = layout
# Start of the class functions
# Close button click handler
def OnCloseButtonClick(self, sender, e):
#self.m_labelProfile.Text == "":
self.Close(False)
# OK button click handler
def OnOKButtonClick(self, sender, e):
if self.m_labelProfile.Text == "":
self.Close(False)
else:
self.Close(True)
## End of Dialog Class ##
def StartForm():
dialog = SampleEtoRadioButtonDialog();
rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
if __name__ == "__main__":
StartForm()
Here is an update of your script that adds a place on the Dialog to store the value selected, and then reacts to changes, and finally prints the selected value on close.
#! python3
# Imports
import Rhino
import scriptcontext
import System
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
# Sample Eto Radio Button class
class SampleEtoRadioButtonDialog(forms.Dialog[bool]):
# Dialog box Class initializer
def __init__(self):
super().__init__()
# Initialize dialog box===================================Window Title
self.Title = 'Test Radio Button'
self.Padding = drawing.Padding(10)
self.Resizable = True
#===========================================================Buttons
# Create the default button
self.DefaultButton = forms.Button()
self.DefaultButton.Text ='OK'
self.DefaultButton.Click += self.OnOKButtonClick
#====================================================Create Radio Button
self.m_radiobuttonlist = forms.RadioButtonList()
self.m_radiobuttonlist.DataStore = ['Option 1', 'Option 2']
self.m_radiobuttonlist.Orientation = forms.Orientation.Vertical
self.m_radiobuttonlist.SelectedIndex = 0
self.m_radiobuttonlist_ExpGroup=forms.RadioButtonList()
# Create Place to Hold Selected Value
self.SelectedOptionValue = self.m_radiobuttonlist.SelectedValue
# React to Option Changes, to Update Selected Value
self.m_radiobuttonlist.SelectedIndexChanged += self.OnRadioButtonListSelectedValueChanged
# Create controls for the dialog==============Create Radio Button Label
self.m_labelProfile = forms.Label()
self.m_labelProfile.Text = 'Select an Option:'
self.m_textboxProfile = forms.TextBox()
self.m_textboxProfile.Text = ""
# Create the abort button
self.AbortButton = forms.Button()
self.AbortButton.Text ='Cancel'
self.AbortButton.Click += self.OnCloseButtonClick
# Create a table layout and add all the controls
layout = forms.DynamicLayout()
layout.Spacing = drawing.Size(30, 10)
layout.AddRow(self.m_labelProfile, self.m_radiobuttonlist)
layout.AddRow(self.DefaultButton, self.AbortButton)
# Set the dialog content
self.Content = layout
# Start of the class functions
# Close button click handler
def OnCloseButtonClick(self, sender, e):
self.Close(False)
# OK button click handler
def OnOKButtonClick(self, sender, e):
if self.m_labelProfile.Text == "":
self.Close(False)
else:
self.Close(True)
def OnRadioButtonListSelectedValueChanged(self, sender, e):
if isinstance(sender, forms.RadioButtonList):
self.SelectedOptionValue = sender.SelectedValue
## End of Dialog Class ##
def StartForm():
dialog = SampleEtoRadioButtonDialog();
rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
# Print SelectedValue
print(dialog.SelectedOptionValue)
if __name__ == "__main__":
StartForm()