Setting the default values in RangeSlider

Question:

I am looking for a way to set the initial, default values to the RangeSlider, which should correspond to the slider handles being set at those positions when the GUI is initialized.

For instance, I set the range of the slider from 0 to 100, but would like to set the default values on the sliders to be 20 and 80. Now the handles at the start are positioned at the min and max values of the slider.

I looked through the documentation for the RangeSlider module, but could not find any option to do so (like in the way it is done in the slider module: self.slider.set(20)).

Here is the example code:

from RangeSlider.RangeSlider import RangeSliderH

from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry("750x200")

hVar1 = DoubleVar()  # left handle variable
hVar2 = DoubleVar()  # right handle variable

rs1 = RangeSliderH(root, [hVar1, hVar2], Width=400, Height=65, min_val=0, max_val=100, padX=17, show_value= True)
rs1.pack()

root.mainloop()

I tried passing some values to variables hVar1 and hVar2 by using hVar1 = tk.DoubleVar(value=20) but had no luck.

Asked By: ProR

||

Answers:

According to the source code, you need to call the private method __moveBar() to set the position of the two slider handles.

Note that private method __moveBar() will be named _RangeSliderH__moveBar() if it is accessed outside the class.

So for your case (min_val=0 and max_val=100):

rs1._RangeSliderH__moveBar(0, 0.2)   # 0.2 means 20 for range 0 to 100
rs1._RangeSliderH__moveBar(1, 0.8)   # 0.8 means 80 for range 0 to 100

Note that you need to calculate the float value if the range is not from 0 to 100.

Output when the application starts:

enter image description here

Answered By: acw1668
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.