title: How can I open and close a new window from main window in PyQt?
body: <p>I searched in Google usually many posts are talking about how to open a new window and not indicating how to close the window. I found several posts in this website but most of them are using dialog window which is not considered in my software.</p>
  
  <p>I make a UI, which contains a spin box and a button, to demonstrate my problem. I can type in a number equal or less than 5 in spin box. When I click the button, a number of new windows will show up and how many windows will show up is depending on the number in the spin box. If I change to the number in spin box then click the button, the original windows will be closed and new windows will be shown.</p>
  
  <p>Fox example, first I type in "2" in spin box then click the button. Then 2 new windows will show up. If I change the number in spin box to 3 then click the button, the original 2 windows will be closed and 3 new windows will show up.</p>
  
  <p>Here is my main program code:</p>
  
  <pre><code>from PyQt5.QtWidgets import QApplication, QMainWindow
  from uitest_review import Ui_MainWindow  # import the UI module
  
  # set up a class for main window
  class window(QMainWindow):
      def __init__(self, parent=None):
          super(window, self).__init__(parent)
  
          self.ui = Ui_MainWindow()
          self.ui.setupUi(self)
          self.ui.Open.clicked.connect(self.openwindow)
  
      def openwindow(self):
          windownum = self.ui.windownum.value()
          print("open window num:", windownum)
          opennewwindow = newwindow(self)
          opennewwindow.show()
  
  class newwindow(QMainWindow):
      def setupUi(self, MainWindow):
      MainWindow.setObjectName("MainWindow")
  
  if __name__ == "__main__":
      app = QApplication([])
      gui = window()
      gui.show()
      app.exec_()
  </code></pre>
  
  <p>Here is my UI code:</p>
  
  <pre><code>from PyQt5 import QtCore, QtGui, QtWidgets
  
  class Ui_MainWindow(object):
      def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(816, 577)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName("centralwidget")
          self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
          self.gridLayout.setObjectName("gridLayout")
          self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
          self.scrollArea.setWidgetResizable(True)
          self.scrollArea.setObjectName("scrollArea")
          self.scrollAreaWidgetContents = QtWidgets.QWidget()
          self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 796, 537))
          self.scrollAreaWidgetContents.setObjectName(\
          "scrollAreaWidgetContents")
          self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
          self.verticalLayout.setContentsMargins(0, 0, 0, 0)
          self.verticalLayout.setObjectName("verticalLayout")
          spacerItem = QtWidgets.QSpacerItem(20, 10, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
          self.verticalLayout.addItem(spacerItem)
          self.windownum = QtWidgets.QSpinBox(self.scrollAreaWidgetContents)
          self.windownum.setMaximum(5)
          self.windownum.setObjectName("windownum")
          self.verticalLayout.addWidget(self.windownum)
          self.groupBox = QtWidgets.QGroupBox(self.scrollAreaWidgetContents)
          self.groupBox.setTitle("")
          self.groupBox.setObjectName("groupBox")
          self.horizontalLayout = QtWidgets.QHBoxLayout(self.groupBox)
          self.horizontalLayout.setObjectName("horizontalLayout")
          spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
          self.horizontalLayout.addItem(spacerItem1)
          self.Open = QtWidgets.QPushButton(self.groupBox)
          font = QtGui.QFont()
          font.setPointSize(14)
          font.setBold(True)
          font.setWeight(75)
          self.Open.setFont(font)
          self.Open.setObjectName("Open")
          self.horizontalLayout.addWidget(self.Open)
          spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
          self.horizontalLayout.addItem(spacerItem2)
          self.verticalLayout.addWidget(self.groupBox)
          self.scrollArea.setWidget(self.scrollAreaWidgetContents)
          self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
          MainWindow.setCentralWidget(self.centralwidget)
          self.statusbar = QtWidgets.QStatusBar(MainWindow)
          self.statusbar.setObjectName("statusbar")
          MainWindow.setStatusBar(self.statusbar)
  
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
          self.Open.setText(_translate("MainWindow", "Open"))
  
  
  if __name__ == "__main__":
      import sys
      app = QtWidgets.QApplication(sys.argv)
      MainWindow = QtWidgets.QMainWindow()
      ui = Ui_MainWindow()
      ui.setupUi(MainWindow)
      MainWindow.show()
      sys.exit(app.exec_())
  </code></pre>
  
  <p>I create a new class (newwindow) in my main program and I can call this class to show a new window. But I cannot figure out how to detect how many windows is opened and how to close them. Does anyone can help me? Thank you so much.</p>