title: Having loop breaks Listbox?
body: <p>I'm making a music player (which I've mostly done) and I've just made it so that it automatically plays the next song when the song has finished (this is what the <code>def checkFinished()</code> does.)</p>
  
  <p>The issue I have is that within <code>checkFinished()</code> I use a loop (to see if the song has finished or not), and the loop seems to break the Tkinter Listbox - I assume it's the loop as removing the loop fixes the issue. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>import os
  from tkinter.filedialog import askdirectory
  
  import pygame
  from mutagen.id3 import ID3
  from tkinter import *
  
  root = Tk()
  root.minsize(300,300)
  
  
  listofsongs = []
  realnames = []
  
  v = StringVar()
  songlabel = Label(root,textvariable=v,width=35)
  
  index = 0
  
  
  def updatelabel():
      global index
      global songname
      v.set(realnames[index])
  
  def nextSong(event):
      global index
      index += 1
      pygame.mixer.music.load(listofsongs[index])
      pygame.mixer.music.play()
      updatelabel()
  
  def prevSong(event):
      global index
      index -= 1
      pygame.mixer.music.load(listofsongs[index])
      pygame.mixer.music.play()
      updatelabel()
  
  def stopSong(event):
      pygame.mixer.music.stop()
      v.set("")
  
  def playSong(event):
      pygame.mixer.music.play()
      print("Now playing {}".format(listofsongs[index]))
  
  def checkFinished(finished):
      while True:
          for event in pygame.event.get():
              if event.type == finished:
                  print("Finished {}\n\n".format(listofsongs[index]))
                  nextSong(event)
  
  
  def directorychooser():
  
      directory = askdirectory()
      os.chdir(directory)
  
      for files in os.listdir(directory):
          if files.endswith(".mp3"):
  
              realdir = os.path.realpath(files)
              try:
                  audio = ID3(realdir)
                  realnames.append(audio['TIT2'].text[0])
              except:
                  realnames.append(files)
              listofsongs.append(files)
  
      pygame.init()
      pygame.mixer.init()
      clock = pygame.time.Clock()
      finished = pygame.USEREVENT + 1
      pygame.mixer.music.set_endevent(finished)
      pygame.mixer.music.load(listofsongs[0])
      pygame.mixer.music.play()
      print("\n\nNow playing {}".format(listofsongs[0]))
      checkFinished(finished)
  
  
  
  directorychooser()
  
  
  
  label = Label(root,text='Music Player',width="20",height="5")
  label.pack()
  
  listbox = Listbox(root,width="80")
  listbox.pack()
  
  
  realnames.reverse()
  
  for items in realnames:
      listbox.insert(0,items)
  
  realnames.reverse()
  
  
  playButton = Button(root,text="Play Song",width="15")
  playButton.pack()
  
  stopButton = Button(root,text="Stop Song",width="15")
  stopButton.pack()
  
  nextButton = Button(root,text="Next Song",width="15")
  nextButton.pack()
  
  previousButton = Button(root,text="Previous Song",width="15")
  previousButton.pack()
  
  
  playButton.bind("&lt;Button-1&gt;",playSong)
  stopButton.bind("&lt;Button-1&gt;",stopSong)
  nextButton.bind("&lt;Button-1&gt;",nextSong)
  previousButton.bind("&lt;Button-1&gt;",prevSong)
  
  
  songlabel.pack()
  
  
  root.mainloop()
  </code></pre>
  
  <p>Thanks in advance!</p>