根据选中按钮打开和关闭音乐

2024-09-27 07:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个菜单级联,它有一个复选按钮来打开/关闭应用程序的背景音乐(使用pythontkinter)。在

当应用程序运行时(通过根.mainloop()),背景音乐已经在播放,复选按钮旁边有一个勾号,我想要它(指示它打开)。在

当我关闭复选按钮时,由于self.sound_关闭命令。在

问题是当我再次单击按钮(勾号出现)时,声音无法打开。我意识到这是因为我在checkbutton中指定的命令是command=关闭声音(). 但我不知道如何让它在滴答声出现时播放(或取消暂停),当滴答声不在时,声音会暂停。在

# within def __init__(self, master) of the app
self.add_sound()

self._value = IntVar(value=1)

menubar = tk.Menu(master)
master.config(menu=menubar)
filemenu = tk.Menu(menubar)        
filemenu.add_checkbutton(label="Music", onvalue=1, offvalue=0, variable= 
                          self._value, command=self.sound_off)


def add_soud(self):
    pygame.mixer.music.load("Sound.mp3")
    pygame.mixer.music.set_volume(0.2)
    pygame.mixer.music.play(-1)

def sound_off(self):
    pygame.mixer.music.pause()

def sound_on(self):
    pygame.mixer.music.unpause()

#Am I supposed to have some sort of 'if' statement to check if the onvalue is 
#0 or 1?

感谢任何帮助。在


Tags: 命令selfmasteradd声音valuedefmusic

热门问题