how tk.DoubleVar() works?

Viewed 18

hello guys i am working with a code that i cant understand:

current_value = tk.DoubleVar()

def get_current_value(): 
    return '{: .2f}'.format(current_value.get())

def brightness_changed(event):
    pct.set_brightness(get_current_value())

brightness = ttk.Scale(RHS,from_=0,to=100,orient='horizontal',
            command=brightness_changed,variable=current_value)

well, the last line is ok i made it myself, but the other is unknow for me, could you pleases simply tell me what is going on here?

1 Answers

DoubleVar() is used for storing floats. In the code it is used by the Scale widget; each time you slide the scale the widget sets the current_value variable.

The widget also points to an event listener (using command) - a callback function that does something each time the slider moves or settles. As you can see this sets the brightness of pct - whatever that is - using the value in current_value. The function get_current_value seems a bit uneccesary, as the event parameter in brightness_changed contains the value of the slider.

Related