我有一个代码,这是其中的一部分:
import PySimpleGUI as sg
import pygame
class Music:
def __init__(self, file):
self.sound = file
def play(self):
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(self.sound)
pygame.mixer.music.play()
def volchange(volume):
pygame.mixer.music.set_volume(volume) # The set_volume range is from 0.00 to 1.00 (every 0.01)
def isplaying():
return pygame.mixer.music.get_busy()
layout = [
[sg.Button('Play'),
sg.Slider(key = 'volume', range=(0, 100),
orientation='h', size=(10, 15), default_value= 100)]
]
window = sg.Window('Help me', layout)
while True:
event, values = window.read()
if event == 'Play':
path = "song.mp3"
music = Music(path)
music.play()
if Music.isplaying():
Music.volchange(float(values['volume'] / 100))
我希望在移动 slider 时实时改变音频的音量。 当我放的时候
if = Music.isplaying():
Music.volchange(values['volume'] / 100)
进入循环while True:
没有任何效果。但正如我注意到的,每次按下“播放”按钮时,这个循环都会继续运行。
如何在循环中设置 isplaying
检查,以便它始终有效?
附注我尽力用英语。
最佳答案
如果 enable_events = True
未启用事件, slider 不会引发事件。
您的应用程序将等待,直到按下播放按钮。请参阅Slider Element .
layout = [
[sg.Button('Play'),
sg.Slider(key = 'volume', range=(0, 100),
orientation='h', size=(10, 15), default_value= 100,
enable_events = True)] # <--- enable slider moved event
]
关于python - 移动 slider 时更改音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59775588/