title: Python Qt - Use button via function from outside a class
body: <p>I am quite new to python. For my problem I cannot seem to find an answer. And sadly I didn't find anything that could held me. I try to call a function from another file in the same folder but it won't work. If I call the same function from the same file, it works just fine. The same goes for external classes.
  This code works:</p>
  
  <pre><code>import sys
  from PyQt5 import QtWidgets
  from ui.mainwindow import Ui_MainWindow
  
  class MainWindow(QtWidgets.QMainWindow):
      def __init__(self, parent=None):
          QtWidgets.QMainWindow.__init__(self, parent)
          self.ui = Ui_MainWindow()
          self.ui.setupUi(self)
          self.show()
  
          self.ui.pushButton.clicked.connect(test_function)
  
  
  def test_function(): 
      window.ui.stackedWidget.setCurrentIndex(1)
  
  if __name__=='__main__':
      app = QtWidgets.QApplication(sys.argv)
      window = MainWindow()
      sys.exit(app.exec_())
  </code></pre>
  
  <p>The following code won't work:</p>
  
  <pre><code>import sys
  from PyQt5 import QtWidgets
  from ui.mainwindow import Ui_MainWindow
  from other_file import test_function
  
  class MainWindow(QtWidgets.QMainWindow):
      def __init__(self, parent=None):
          QtWidgets.QMainWindow.__init__(self, parent)
          self.ui = Ui_MainWindow()
          self.ui.setupUi(self)
          self.show()
  
          self.ui.pushButton.clicked.connect(test_function)
  
  
  
  if __name__=='__main__':
      app = QtWidgets.QApplication(sys.argv)
      window = MainWindow()
      sys.exit(app.exec_())
  </code></pre>
  
  <blockquote>
    <p>other_file.py</p>
  </blockquote>
  
  <pre><code>def test_function():
      window.ui.stackedWidget.setCurrentIndex(1)
  </code></pre>
  
  <p>Error: cannot import name 'test_function'</p>