Home > front end >  Can't change the color of the numbers in RangeSlider library with Tkinter
Can't change the color of the numbers in RangeSlider library with Tkinter

Time:09-02

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 ?

CodePudding user response:

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
  • Related