title: How to use multi-threading to play/pause BG Music in tkinter app?
body: <p>A part of what I am doing, needs me to have <code>background music</code> in a <code>tkinter</code> game I created long time ago. I am using <code>playsound.playsound</code>{ <a href="https://pypi.org/project/playsound/" rel="nofollow noreferrer">Docs Here</a> } to play music . I could use any other tool if needed to achieve what I intend like <code>pyglet.media</code> or <code>pygame.mixer</code>.
  As the actual program was about 1k lines, I have tried adding an <a href="https://www.stackoverflow.com/help/minimal-reproducible-example">MCVE</a> below.</p>
  <h1>Desired behavior &amp; issue</h1>
  <p>The BGM (Background Music) should start when the app is launched - of course alongside a GUI, I would have a <code>button</code> to stop the BGM OR more preferably pause/play - I do think I need to use anything other than <code>playsound.playsound</code> for pause/play behavior.</p>
  <p><strong>The issue::</strong> I can't seem to figure out how to make that <code>communication</code> between both the <code>threads</code>  so that I can stop the music from playing or perhaps <code>terminate</code> the thread playing BGM - I could create a new thread when needed.</p>
  <h1>What I Have Tried</h1>
  <p>First up, I created two classes for GUI and BGM, each <code>inheriting</code> from <code>threading.Thread</code> - overridden the <code>constructor</code> and <code>run()</code> methods to do what I intend. Like this -</p>
  <pre><code>import tkinter as tk
  import threading
  from playsound import playsound
  
  class BGM(threading.Thread):
      def __init__(self, stopSignal):
          threading.Thread.__init__(self)
          self.stopSignal = stopSignal
  
      def run(self):
          while not self.stopSignal:
              playsound('LoL.mp3') # to make this function play asynchronously, pass in False as second arg.
  
  class GUI(threading.Thread):
      def __init__(self):
          threading.Thread.__init__(self)
          self.root = tk.Tk()
          
      def run(self):
          tk.Button(self.root, text='stop').pack()
  
  if __name__ == '__main__':        
      guiThread = GUI()
      guiThread.start()
      musicThread = BGM(0)
      musicThread.start()
      guiThread.root.mainloop()
  </code></pre>
  <p>But as you can see, it will just continue to play the song infinitely as long as the app is alive. There is no communication ( or active sync ) between GUI ( the button to stop song ) and BGM thread.</p>
  <p>Now I tried using <code>threading.Event()</code> objects. The <a href="https://docs.python.org/3/library/threading.html#event-objects" rel="nofollow noreferrer">official docs on threading.Event()</a> quotes:</p>
  <blockquote>
  <p>This is one of the simplest mechanisms for communication between
  threads: one thread signals an event and other threads wait for it.</p>
  <p>An event object manages an internal flag that can be set to true with
  the set() method and reset to false with the clear() method. The
  wait() method blocks until the flag is true.</p>
  </blockquote>
  <p>I'm not sure if this is useful for me to achieve what I intend but using <code>threading.Event()</code> did not do anything for me (or maybe I wasn't able to make use of it properly)</p>
  <h1>Now</h1>
  <p>What would you suggest to do to implement a BGM thread which can be stopped ( or paused/played preferably ) with a button(s) in the GUI thread.</p>
  <p>Thank You For Any help Or Suggestions :)</p>