2

我有一个代码,这是其中的一部分:

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))

我希望在移动滑块时音频的音量实时变化。当我放

if = Music.isplaying():
    Music.volchange(values['volume'] / 100)

进入循环while True:没有任何作用。但正如我注意到的,每次按下“播放”按钮时,这个循环都会继续运行。如何isplaying在循环中设置检查以使其始终有效?

PS我尽量用英语。

4

1 回答 1

0

如果事件未被启用,则滑块不会引发事件enable_events = True
您的应用程序一直等到按下播放按钮。请参阅滑块元素

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
]
于 2020-01-16T18:39:17.027 回答