Eto Forms with Toggle

I’m working on a Eto form with GhPython and managed to define the correct behaviour using a Button, however I need it to work with a Toggle as shown below:

At this point, if I connect the Toggle it will close and reopen with default values everytime controls are changed.
Does anyone knows how to refactor the code in order to keep the same behaviour usung the Toggle instead of the Button?
Here’s the code

import Rhino
import System
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
import Eto
import scriptcontext as sc

class OptionsPanel(forms.Form):

    def __init__(self):
 
        self.m_slider = forms.Slider()
        self.m_slider.TickFrequency = 10
        self.m_slider.MinValue = 1
        self.m_slider.MaxValue = 100
        self.m_slider.Value = x
        self.m_slider.ValueChanged += self.OnSliderValueChanged
        self.m_numeric_updown = forms.NumericUpDown()
        self.m_numeric_updown.DecimalPlaces = 0
        self.m_numeric_updown.Increment = 1
        self.m_numeric_updown.MaxValue = 100.0
        self.m_numeric_updown.MinValue = 1.0
        self.m_numeric_updown.Value = x
        self.m_numeric_updown.ValueChanged += self.OnNumericChanged
        self.o_slider = forms.Slider()
        self.o_slider.TickFrequency = 10
        self.o_slider.MinValue = 1
        self.o_slider.MaxValue = 60
        self.o_slider.Value = y
        self.o_slider.ValueChanged += self.OnSliderValueChanged
        self.o_numeric_updown = forms.NumericUpDown()
        self.o_numeric_updown.DecimalPlaces = 0
        self.o_numeric_updown.Increment = 1
        self.o_numeric_updown.MaxValue = 60
        self.o_numeric_updown.MinValue = 1.0
        self.o_numeric_updown.Value = y
        self.o_numeric_updown.ValueChanged += self.OnNumericChanged
 
        self.o_numeric_updown.ValueBinding.Bind(self.o_slider, "Value", Eto.Forms.DualBindingMode.TwoWay)
        self.m_numeric_updown.ValueBinding.Bind(self.m_slider, "Value", Eto.Forms.DualBindingMode.TwoWay)
 
        self.m_ok = forms.Button(Text = 'OK')
        self.m_ok.Click += self.OnButtonOKClick
        self.m_cancel = forms.Button(Text = 'Cancel')
        self.m_cancel.Click += self.OnButtonCClick
 
        self.Initialize()
        self.CreateFormControls()
 
    def Initialize(self):
        self.Padding = drawing.Padding(10)
        self.Resizable = False
        self.Topmost = True
 
    def CreateFormControls(self):
        layout = forms.DynamicLayout()
        layout.Spacing = drawing.Size(5, 5)
        layout.AddRow(self.m_slider, self.m_numeric_updown)
        layout.AddRow(None)
        layout.AddRow(self.o_slider, self.o_numeric_updown)
        layout.AddRow(None)
        layout.AddRow(self.m_ok, self.m_cancel)
        layout.AddRow(None)
        self.Content = layout
 
    def OnSliderValueChanged(self, sender, e):
        ghenv.Component.ExpireSolution(True)
 
    def OnNumericChanged(self, sender, e):
        ghenv.Component.ExpireSolution(True)
 
    def OnButtonOKClick(self, sender, e):
        form.Close()
 
    def OnButtonCClick(self, sender, e):
        form.Close()
        self.m_numeric_updown.Value = x
        self.o_slider.Value = y

    def GetNumber1(self):
        return self.m_numeric_updown.Value
 
    def GetNumber2(self):
        return self.o_slider.Value
 
if z:
    if 'form' in globals():
        form.Close()
    form = OptionsPanel();
    form.Show()

if 'form' in globals():
    value1 = form.GetNumber1()
    value2 = form.GetNumber2()
if 'value1' not in globals():
    value1 = x
if 'value2' not in globals():
    value2 = y
a = value1
b = value2

Thanks in advance

1 Like

Managed to fix it by looking into other examples here in the forum.
Not sure if it’s the best approach but it’s working. Here’s the code in case someone find it useful

import System
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
import Eto
import scriptcontext as sc

class OptionsPanel(forms.Form):

   def __init__(self):

       self.m_slider = forms.Slider()
       self.m_slider.TickFrequency = 10
       self.m_slider.MinValue = 1
       self.m_slider.MaxValue = 100
       self.m_slider.Value = x
       self.m_slider.ValueChanged += self.OnSliderValueChanged
       self.m_numeric_updown = forms.NumericUpDown()
       self.m_numeric_updown.DecimalPlaces = 0
       self.m_numeric_updown.Increment = 1
       self.m_numeric_updown.MaxValue = 100.0
       self.m_numeric_updown.MinValue = 1.0
       self.m_numeric_updown.Value = x
       self.m_numeric_updown.ValueChanged += self.OnNumericChanged
       self.o_slider = forms.Slider()
       self.o_slider.TickFrequency = 10
       self.o_slider.MinValue = 1
       self.o_slider.MaxValue = 60
       self.o_slider.Value = y
       self.o_slider.ValueChanged += self.OnSliderValueChanged
       self.o_numeric_updown = forms.NumericUpDown()
       self.o_numeric_updown.DecimalPlaces = 0
       self.o_numeric_updown.Increment = 1
       self.o_numeric_updown.MaxValue = 60
       self.o_numeric_updown.MinValue = 1.0
       self.o_numeric_updown.Value = y
       self.o_numeric_updown.ValueChanged += self.OnNumericChanged

       self.o_numeric_updown.ValueBinding.Bind(self.o_slider, "Value", Eto.Forms.DualBindingMode.TwoWay)
       self.m_numeric_updown.ValueBinding.Bind(self.m_slider, "Value", Eto.Forms.DualBindingMode.TwoWay)


       self.Initialize()
       self.CreateFormControls()

   def Initialize(self):
       self.Padding = drawing.Padding(10)
       self.Resizable = False
       self.Topmost = True
       self.Closed += self.OnFormClosed

   def CreateFormControls(self):
       layout = forms.DynamicLayout()
       layout.Spacing = drawing.Size(5, 5)
       layout.AddRow(None)
       layout.AddRow(self.m_slider, self.m_numeric_updown)
       layout.AddRow(None)
       layout.AddRow(self.o_slider, self.o_numeric_updown)
       layout.AddRow(None)
       self.Content = layout

   def OnSliderValueChanged(self, sender, e):
       ghenv.Component.ExpireSolution(True)

   def OnNumericChanged(self, sender, e):
       ghenv.Component.ExpireSolution(True)


   def GetNumber1(self):
       return self.m_numeric_updown.Value

   def GetNumber2(self):
       return self.o_slider.Value
       
   def OnFormClosed(self, sender, e):
       if sc.sticky.has_key('form'):
           form = sc.sticky['form']
           if form:
               form.Dispose()
               form = None
           sc.sticky.Remove('form')

def Dostuff():
   if sc.sticky.has_key('form'):
       return
   form = OptionsPanel();
   form.Show()
   sc.sticky['form'] = form

if z == True:
   Dostuff()

if sc.sticky.has_key('form'):
   form = sc.sticky['form']
   value1 = form.GetNumber1()
   value2 = form.GetNumber2()
else:
   value1 = x
   value2 = y

a = value1
b = value2

1 Like

Just wanted to let you know that your example is literally what I had been searching for. Had just started creating my custom ui in grasshopper (Human UI is nice but too much spaghetti and slows down my complex gh script)

So your script is a godsend. Wish python eto examples were more accessible. Most are only limited to creating elements but not how to use the values.