Eto Slider - Showing current Value

Hi!
I’m using Eto in a Python script to get user input. I am using the slider to choose a value, but since the slider only shows ‘ticks’ it’s hard to be sure which value was chosen. I managed to show the value at the beginning, but not to update it as the slider is moved.

How could I show the current slider-value in the Eto form as the slider is being used?
Alternatively, would there be a way to show actual values instead of simple ticks?

Sounds like you just need to write an event handler have the slider “listen” to it (line 20 of the code)

import Eto.Forms as forms

def OnSliderChanged(s,e):
    global lbl
    lbl.Text = str(s.Value)

ef = forms.Form(Topmost = True)
ef.Width = 300
ef.Height = 200
layout = forms.StackLayout(
    Padding = 20,
    Orientation = forms.Orientation.Horizontal
    )
sl = forms.Slider(
    MaxValue = 20,
    MinValue = 1,
    Value = 5,
    Width = 200
    )
sl.ValueChanged += OnSliderChanged
lbl = forms.Label(Text=str(sl.Value))
layout.Items.Add(sl)
layout.Items.Add(lbl)
ef.Content = layout

ef.Show()
2 Likes

Thank you, that’s exactly what I was looking for!

I’m a novice and event handling, so this is new to me, very helpful, thank you for taking the time to answer me.

I wasn’t sure at first what the variables (s, e) are that OnSliderChanged needed, and why it can be run without supplying those variables. I suppose it’s specific to event handling? What do those variables represent?

I can’t speak for event handling within python itself but Eto and the rhinocommon sdk are .NET libraries
Events like Slider.ValueChanged sends a call to the handler, passing the sender object (s) and a collection of event arguments (e) into it.

Can someone please help me with how I can use the function OnSliderChangeEvent inside a class?
What I want is that the dynamic 9Current) value when I move the slider must be displayed as a Text box just like how @Will_Wang has demonstrated for Grasshopper Environment.

@stevebaer :any help

Also,I am using the RhinoPython editor, and below is my code.

import Rhino
import Eto
import Eto.Drawing as drawing
import Eto.Forms as forms


class EtoFormSliderTest(forms.Dialog[bool]):
    def __init__(self):        
        self.Title = "Sample Slider Eto Test"
        self.Padding = drawing.Padding(5)
        self.ClientSize = drawing.Size(300,50)
        self.Resizable = True
        
        self.sliderTest = forms.Slider()
        self.sliderTest.MinValue = 1
        self.sliderTest.MaxValue = 50
        self.sliderTest.Value = 5
        self.sliderTest.ValueChanged += self.OnSliderValueChanged
        self.sliderLabelTest = forms.Label(Text = str(self.sliderTest.Value))
        
        layout= forms.DynamicLayout()
        layout.AddRow(None)
        layout.AddRow(self.sliderTest)
        layout.AddRow(self.sliderLabelTest)
        
        self.Content = layout
        
    def OnSliderValueChanged(self,sender,e):
        print(self.sliderTest.Value)
        return self.sliderTest.Value
        
def OpenEtoWindow():
    form = EtoFormSliderTest()
    Rhino.UI.EtoExtensions.ShowSemiModal(form, Rhino.RhinoDoc.ActiveDoc, Rhino.UI.RhinoEtoApp.MainWindow)

if __name__ == '__main__':
    OpenEtoWindow()


Finally, I was able to solve my own problem. Here is the code :slight_smile:
#GoOpenSource

import Rhino
import Eto
import Eto.Drawing as drawing
import Eto.Forms as forms


class EtoFormSliderTest(forms.Dialog[bool]):
    def __init__(self):        
        self.Title = "Sample Slider Eto Test"
        self.Padding = drawing.Padding(5)
        self.ClientSize = drawing.Size(300,50)
        self.Resizable = True
        
        self.sliderTest = forms.Slider()
        self.sliderTest.MinValue = 1
        self.sliderTest.MaxValue = 50
        self.sliderTest.Value = 5
        self.sliderTest.ValueChanged += self.OnSliderValueChanged
        self.sliderLabelTest = forms.Label(Text = str(self.sliderTest.Value))
        
        layout= forms.DynamicLayout()
        layout.AddRow(None)
        layout.AddRow(self.sliderTest)
        layout.AddRow(self.sliderLabelTest)
        
        self.Content = layout
        
    def OnSliderValueChanged(self,sender,e):
        print(self.sliderTest.Value)
        self.sliderLabelTest.Text = str(self.sliderTest.Value)

        
def OpenEtoWindow():
    form = EtoFormSliderTest()
    Rhino.UI.EtoExtensions.ShowSemiModal(form, Rhino.RhinoDoc.ActiveDoc, Rhino.UI.RhinoEtoApp.MainWindow)

if __name__ == '__main__':
    OpenEtoWindow()
2 Likes