Creating a volume slider in tkinter

4.3k views Asked by At

I'm trying to use .get() to access entry from my Scale but the volume doesn't change when I move slider. I'm using pygame mixer too btw.

Relevant code;

def change_vol():
    sounds.music.set_volume(vol.get())
#########################################
def play_music():
    load_music()
    sounds.music.play()
    pass
##########################
def load_music():
    sounds.music.load("fakeplastic.wav")


vol = Scale(
    sound_box,
    from_ = 0,
    to = 1,
    orient = HORIZONTAL ,
    resolution = .1,
)
vol.grid(row = 3, column = 1)

If you'd like me to post any other snippets please ask, thanks.

1

There are 1 answers

3
AudioBubble On BEST ANSWER

It looks like you forgot to assign the Scale's command option to change_vol so that the function is called each time you adjust the scale:

vol = Scale(
    sound_box,
    from_ = 0,
    to = 1,
    orient = HORIZONTAL ,
    resolution = .1,
    ####################
    command=change_vol
    ####################
)

You will also need to redefine change_vol to accept the event object that will be sent to it every time you move the scale:

def change_vol(_=None):
    sounds.music.set_volume(vol.get())

I did _=None to make it clear that you are not using the argument inside the function.