【问题标题】:Changing the volume when moving the slider移动滑块时更改音量
【发布时间】:2020-01-16 18:14:43
【问题描述】:

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

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 签入,使其始终有效?

附:我尽量用英语。

【问题讨论】:

    标签: python pygame pysimplegui


    【解决方案1】:

    如果enable_events = True 未启用事件,滑块不会引发事件。
    您的应用程序一直等到按下播放按钮。见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
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多