Can't change the color of the numbers in RangeSlider library with Tkinter

Question:

It’s not the slider from tkinter so it would explain why I can’t use fg or foreground color even in configure or in a style. I checked the documentation and nothing about chaging the numbers color was found (it doesn’t figure in the attributes)

https://github.com/harshvinay752/RangeSlider

hVar1 = DoubleVar()  #left handle variable
hVar2 = DoubleVar()  #right handle variable
rs1 = RangeSliderH(root , [hVar1, hVar2],bar_color_outer='blue' ,bgColor='red', 
line_s_color='green', line_color='gray',  Width=400, Height=65,padX=17 ,min_val=0, max_val=20, 
show_value= True ) #horizontal

How I can do this ? Any solution ?

Asked By: jush456

||

Answers:

You can create a custom class derived from RangeSliderH and add value_color option to set the color of the shown values inside the override function getValues():

...

class MyRangeSliderH(RangeSliderH):
    def __init__(self, master, variables, **kwargs):
        self.value_color = kwargs.pop('value_color', 'black')
        super().__init__(master, variables, **kwargs)

    # override getValues()
    def getValues(self):
        if self.show_value:
            # update the color of the shown values
            for bar in self.bars:
                self.canv.itemconfig(bar['Ids'][-1], fill=self.value_color)
        return super().getValues()

...

# Use MyRangeSliderH instead with given value_color
rs1 = MyRangeSliderH(root , [hVar1, hVar2],bar_color_outer='blue' ,bgColor='red',
line_s_color='green', line_color='gray',  Width=400, Height=65,padX=17 ,min_val=0, max_val=20,
show_value= True, value_color='yellow') #horizontal
...

Update: If you don’t want to use class, you can use tkinter variable tracing using .trace_add() to update the color whenever the values are changed:

hVar1 = DoubleVar()  #left handle variable
hVar2 = DoubleVar()  #right handle variable
rs1 = RangeSliderH(root , [hVar1, hVar2],bar_color_outer='blue' ,bgColor='red',
line_s_color='green', line_color='gray',  Width=400, Height=65,padX=17 ,min_val=0, max_val=20,
show_value= True ) #horizontal

def update_value_color(slider, color='yellow'):
    if slider.show_value:
        for bar in slider.bars:
            slider.canv.itemconfig(bar['Ids'][-1], fill=color)

hVar1.trace_add('write', lambda *_: update_value_color(rs1))
update_value_color(rs1) # update the value color initially
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.