title: PyQt - Displaying GIF While Other Function Runs
body: <p>I have an app that saves data to a file, and sometimes the save function takes quite awhile since my file is large. I wanted to have a "loading.gif" displayed while the file is being saved, but haven't figured out how to do so. Here is my code:</p>
  
  <pre><code>def saveBook(self):
      self.loadingWidget.setHidden(not root.journal.status.loading.isHidden())
      app.processEvents()
      self.excelSheet.save(filename = self.file)
      self.loadingWidget.setHidden(not root.journal.status.loading.isHidden())
  </code></pre>
  
  <p>LoadingWidget is the QLabel holding the GIF (which is automatically turned on). </p>
  
  <ul>
  <li>The first line turns the visibility of the GIF on</li>
  <li>The second line processes/displays the GIF immediately</li>
  <li>The third runs the save function (for however long)</li>
  <li>The fourth line turns the GIF back off. </li>
  </ul>
  
  <p>The GIF is displayed, but it's paused. If I omit the fourth line, the GIF will play after the save function is finished, but I can't make the GIF play <em>while</em> the save function is running. How do I go about doing this?</p>
  
  <p>Edit: Suggested duplicate is incorrect. That person was trying to display the gif alongside a label. I'm trying to have the GIF frame play while another function, <code>save</code>, runs. The GIF is already visible, but the app is not processing its playing until after the current <code>save</code> function is done.</p>
  
  <p>Update - Solution:</p>
  
  <pre><code>def saveBook(self):
      #Show GIF
      root.saved.gif.on()
      #Update frame
      GLOB.app.processEvents()
      #Save on separate thread to allow interface to remain active
      t = threading.Thread(target=self.saveThread)
      t.start()
  
  def saveThread(self): #this function runs in background, allowing GIF to run in main thread
      #Prevent multiple threads from queueing to save the file
      if not self.lock.acquire(False):
          return
      self.b.save(self.file)
      #Remove GIF
      root.saved.gif.off()
      self.lock.release()
  </code></pre>