基于 CheckButton 打开和关闭音乐

Toggling Music On and Off Based on CheckButton

我有一个菜单级联,其中有一个用于将 on/off 背景音乐切换到应用程序的复选按钮(使用 python tkinter)。

当应用程序 运行(通过 root.mainloop())时,背景音乐已经在播放,并且复选框旁边有一个勾号,就像我想要的那样(表示它打开)。

当我关闭复选按钮时,声音因 self.sound_off 命令而关闭。

问题是当我再次点击按钮时(勾号出现),声音没有打开。我意识到这是因为我在 checkbutton 中指定的命令是 command=sound.off()。但我不确定如何做到当滴答声出现时声音播放(或取消暂停)而当滴答声不存在时声音暂停。

# 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?

感谢任何帮助。

这行得通

filemenu.add_checkbutton(label="Music", onvalue=1, offvalue=0, variable= 
                      self.sound_value, command=self.sound_manipulation)

def sound_manipulation(self):
    if self.sound_value.get() == 1:
        pygame.mixer.music.unpause()
    else:
        pygame.mixer.music.pause()