title: Returning to a previous window
body: <p>I am creating a GUI using Qt creator for my Thesis. I am working on the functions of the buttons to go to a window and return to the previous window. </p>
  
  <p>I have done the function for going to another window.</p>
  
  <p>I am having problem with returning to the previous window. I always get the error <code>formTrain.hide()</code> is not defined.</p>
  
  <p>How can I resolve this error?</p>
  
  <p><strong>Code for main window</strong></p>
  
  <pre><code>import sys
  
  from PyQt5 import QtCore, QtGui, QtWidgets
  
  from testsys_auto import Ui_testsys
  from trainsys_auto import *
  
  class Ui_MainWindow(object):
      def trainsys(self):
       self.trainwin = QtWidgets.QMainWindow()
       self.ui = Ui_trainsys()
       self.ui.setupUi(self.trainwin)
       formMain.hide()
       self.trainwin.show()
     def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(405, 307)
          self.centralWidget = QtWidgets.QWidget(MainWindow)
          self.centralWidget.setObjectName("centralWidget")
          self.verticalLayoutWidget = QtWidgets.QWidget(self.centralWidget)
          self.verticalLayoutWidget.setGeometry(QtCore.QRect(110, 120, 160, 107))
          self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
          self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
          self.verticalLayout.setContentsMargins(0, 0, 0, 0)
          self.verticalLayout.setObjectName("verticalLayout")
          self.btn_TrainSys = QtWidgets.QPushButton(self.verticalLayoutWidget)
          self.btn_TrainSys.setObjectName("btn_TrainSys")
          self.verticalLayout.addWidget(self.btn_TrainSys)
          self.btn_TrainSys.clicked.connect(self.trainsys)
  def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate("MainWindow", "Home Screen"))
          self.btn_TrainSys.setText(_translate("MainWindow", "Train System"))
          self.label.setText(_translate("MainWindow", "Diabetic Retinopathy Detection"))
  
  
  if __name__ == "__main__":
   app = QtWidgets.QApplication(sys.argv)
   formMain = QtWidgets.QMainWindow()
   ui = Ui_MainWindow()
   ui.setupUi(formMain)
   formMain.show()
   sys.exit(app.exec_())
  </code></pre>
  
  <p><strong>Code for <code>trainsys</code> window</strong></p>
  
  <pre><code>import sys
  from PyQt5 import QtCore, QtGui, QtWidgets
  from mainwindow1 import *
  
  class Ui_trainsys(object):
  
     def backhome(self):
          self.backwin = QtWidgets.QMainWindow()
          self.ui = Ui_MainWindow()
          self.ui.setupUi(self.backwin)
          formTrain.hide()
          self.backwin.show()
  
     def setupUi(self, trainsys):
         self.btn_Backtrain = QtWidgets.QPushButton(self.verticalLayoutWidget)
         self.btn_Backtrain.setObjectName("btn_Backtrain")
  
         self.btn_Backtrain.clicked.connect(self.backhome)
      def retranslateUi(self, trainsys):
          _translate = QtCore.QCoreApplication.translate
          trainsys.setWindowTitle(_translate("trainsys", "Train System")
          self.btn_Backtrain.setText(_translate("trainsys", "Back to Home Screen"))
          self.label.setText(_translate("trainsys", "Train System"))
  
  
  
  if __name__ == "__main__":
   app = QtWidgets.QApplication(sys.argv)
   formTrain = QtWidgets.QMainWindow()
   ui = Ui_trainsys()
   ui.setupUi(formTrain)
   formTrain.show()
   sys.exit(app.exec_())
  </code></pre>