简体   繁体   English

Tkinter slider 没有正确控制 Pygame 通道音量

[英]Tkinter slider not properly controlling Pygame channel volume

In my code, I am using the Pygame mixer to play different parts simultaneously, and I am testing adding a slider to only one of the tracks.在我的代码中,我正在使用 Pygame 混音器同时播放不同的部分,并且我正在测试仅将 slider 添加到其中一个轨道。 I used the code below, but the volume is constant throughout the slider, only turning off when it hits zero.我使用了下面的代码,但整个 slider 的音量是恒定的,只有在达到零时才会关闭。 How do I make it that the volume declines gradually?如何使音量逐渐下降?

# Things to note
from tkinter import *
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from pygame import mixer

window = Tk()

# Pygame mixer code    
mixer.init()
mixer.set_num_channels(5)

channel1 = mixer.Channel(0)
channel1Sound = mixer.Sound("bass.wav")
channel1.play(channel1Sound)

channel2 = mixer.Channel(1)
channel2Sound = mixer.Sound("drums.wav")
channel2.play(channel2Sound)

channel3 = mixer.Channel(2)
channel3Sound = mixer.Sound("other.wav")
channel3.play(channel3Sound)

channel4 = mixer.Channel(3)
channel4Sound = mixer.Sound("piano.wav")
channel4.play(channel4Sound)

channel5 = mixer.Channel(4)
channel5Sound = mixer.Sound("vocals.wav")
channel5.play(channel5Sound)

# Slider code
def volume(x):
    channel5.set_volume(vocalsSlider.get())

vocalsSlider = Scale(window, from_ = 0, to=100, orient=HORIZONTAL, command=volume)
vocalsSlider.set(100)
vocalsSlider.pack()

Per pygame documentation on set_volume:根据关于 set_volume 的 pygame 文档:

The value argument is between 0.0 and 1.0.

Divide the value from get() by the max to have a float that will work.将 get() 中的值除以最大值,得到一个可以工作的浮点数。

channel5.set_volume(vocalsSlider.get()/100.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM