2017-10-17 16 views
0

저는 배경 음악을 앱으로 전환하는 데 사용할 수있는 체크 버튼이있는 메뉴 캐스케이드가 있습니다 (Python tkinter 사용).CheckButton을 기반으로 음악 켜고 끄기

앱을 실행할 때 (root.mainloop()을 통해) 백그라운드 음악이 이미 재생되고 있고 체크 버튼 옆에 체크 표시가 있습니다 (켜기를 나타냅니다).

체크 버튼을 끄면 self.sound_off 명령으로 인해 소리가 꺼집니다.

문제는 버튼을 다시 클릭 할 때 (그리고 틱이 나타나는 경우) 소리가 켜지지 않는다는 것입니다. 체크 버튼에 지정된 명령이 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? 

도움을 주시면 감사하겠습니다.

답변

0

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() 
를 작동합니다