title: PyQt5 Calling a static method inside a class' method messes up the execution order of the method's code
body: <p>I've been building a fairly simple program using PyQt5 for the GUI for the past few days and I have the following problem which I can't seem to find a solution for.</p>
  <p>Here's an oversimplified version of my code (kept the absolute basics to keep things short):</p>
  <pre class="lang-py prettyprint-override"><code>def run_blueprint(file):
      # doing stuff to the file and returning the path to the output file
      return full_path
  
  
  class Window(QMainWindow, Ui_MainWindow):
      # some variables here
  
      def __init__(self, parent=None):
          super().__init__(parent)
          self.setupUi(self)
          self.connectSignalsSlots()
  
      def connectSignalsSlots(self):
          self.pushButton.clicked.connect(self.processFiles)
  
      def processFiles(self):
          self.toggleElements()
          for i, file in enumerate(self.selected_files_directories):
              temp_full_path = run_blueprint(file)
              self.listWidget_3.insertItem(i, temp_full_path)
          self.toggleElements()
  
      def toggleElements(self):
          self.pushButton_2.setEnabled(not self.pushButton_2.isEnabled())
          self.listWidget.setEnabled(not self.listWidget.isEnabled())
  
  
  if __name__ == &quot;__main__&quot;:
      app = QApplication(sys.argv)
      win = Window()
      win.show()
      sys.exit(app.exec())
  </code></pre>
  <p>Let's assume that I have selected an item from the <code>listWidget</code> and also have a list of file paths stored in the <code>selected_files_directories[]</code> variable.</p>
  <p>If I run the code as is, when it's time for <code>processFiles(self)</code> to run I get some weird behavior. For some reason the for loop is ran first, then the first <code>toggleElements()</code> and after that the second <code>toggleElements()</code>. This results in the two elements that I want to temporarily disable until the for loop is over staying enabled the whole time.</p>
  <p>However if I don't call the <code>run_blueprint(file)</code> method at all, but instead run any other code inside the for loop or even completely discard the loop, those two elements are disabled first, then the rest of the code runs and finally they are enabled again.</p>
  <p>I believe the problem has something to do with me calling a static method outside the class. Do you have any ideas on how to solve this issue? Thanks in advance!</p>
  <br>
  <p><strong>Edit:</strong> Below is the simplified version of the <code>run_blueprint()</code> function.
  The basic functionality of this program is as follows: I select a template file <code>(.docx)</code> and input file(s) <code>(.pdf)</code>. Then using the <code>docxtpl</code> and <code>pdfplumber</code> libraries, for each of the selected input files I get specific text pieces from them and place them inside the template document and save that as a .docx file.</p>
  <p>In the following code assume that <code>template_path</code> and <code>bp_path</code> show the path to the template file and blueprint file respectively.</p>
  <pre class="lang-py prettyprint-override"><code>def run_blueprint(file):
      doc = DocxTemplate(template_path) # docxtpl method
      global lns
      lns = export_data(file) # function I made that uses pdfplumber to get all the text from a pdf file
  
      global context
      context = {}
  
      with open(bp_path, 'r', encoding='utf8') as f:
          blueprint = f.read() # blueprint contains python code that populates the context variable. I do it this way to easily change between blueprints
      exec(blueprint, globals(), locals()) # I know it's not a good practice to use exec but it's intended for personal use
  
      doc.render(context) # docxtpl method
      output_path = &quot;D:\\Desktop&quot; # could be any path
      doc.save(output_path) # docxtpl method
      return output_path
  </code></pre>
  <p>The blueprint code usually contains API calls that take a few milliseconds to send a response. Apart from that it's mostly <code>regex</code> used to locate the text pieces I'm looking for.</p>