I am trying to change the volume of my music in realtime for my music player.
My code:
JavaScript
x
11
11
1
"""Volume Input"""
2
VolumeLevel = tkr.Scale(player,from_=0,to_=1,
3
orient = tkr.HORIZONTAL, resolution = 0.1)
4
def change_vol(_=None):
5
pygame.mixer.music.set_volume(vol.get())
6
vol = Scale(
7
sound_box,
8
9
10
)
11
And here are the action events:
JavaScript
1
8
1
def Play():
2
pygame.mixer.music.load(playlist.get(tkr.ACTIVE))
3
var.set(playlist.get(tkr.ACTIVE))
4
pygame.mixer.music.play()
5
pygame.mixer.music.set_volume(VolumeLevel.get())
6
print(pygame.mixer.music.get_volume())
7
print(VolumeLevel.get())
8
But I don’t know how to change the volume while the music is playing, it only changes the volume after I restart the song.
I tried:
JavaScript
1
3
1
while pygame.mixer.music.get_busy() is True():
2
pygame.mixer.music.set_volume(VolumeLevel.get())
3
But that didn’t work it just game me an error.
Advertisement
Answer
If you don’t use OOP(and I dont think you do based on what you provided), this should be the solution:
JavaScript
1
6
1
VolumeLevel = tkr.Scale(player,from_=0,to_=1,
2
orient = tkr.HORIZONTAL, command=change_vol, resolution = 0.1)
3
def change_vol(vol, _=None):
4
vol *= 100
5
pygame.mixer.music.set_volume(vol)
6