How to bring up a new window when clicking a Button?

Dear friends, comrades and simply not indifferent people. Not so long ago started to learn Python and PyQt5. I need when clicking on a Button displayed one of the Windows, depending on the selected number of banks. On the Internet anything worthwhile on this issue is not found, and due to the lack of knowledge of anything to offer myself can't. How can I be?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
 MainWindow.resize(679, 324)
 self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
 self.label = QtWidgets.QLabel(self.centralwidget)
 self.label.setGeometry(QtCore.QRect(20, 10, 261, 16))
self.label.setObjectName("label")
 self.comboBox = QtWidgets.QComboBox(self.centralwidget)
 self.comboBox.setGeometry(QtCore.QRect(20, 40, 131, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
 self.label_2 = QtWidgets.QLabel(self.centralwidget)
 self.label_2.setGeometry(QtCore.QRect(20, 100, 151, 16))
self.label_2.setObjectName("label_2")
 self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
 self.comboBox_2.setGeometry(QtCore.QRect(20, 130, 69, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
 self.label_3 = QtWidgets.QLabel(self.centralwidget)
 self.label_3.setGeometry(QtCore.QRect(420, 10, 181, 16))
self.label_3.setObjectName("label_3")
 self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
 self.lineEdit.setGeometry(QtCore.QRect(420, 50, 113, 20))
self.lineEdit.setObjectName("lineEdit")
 self.label_4 = QtWidgets.QLabel(self.centralwidget)
 self.label_4.setGeometry(QtCore.QRect(420, 30, 47, 13))
self.label_4.setObjectName("label_4")
 self.label_5 = QtWidgets.QLabel(self.centralwidget)
 self.label_5.setGeometry(QtCore.QRect(420, 80, 47, 13))
self.label_5.setObjectName("label_5")
 self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
 self.lineEdit_2.setGeometry(QtCore.QRect(420, 100, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
 self.pushButton = QtWidgets.QPushButton(self.centralwidget)
 self.pushButton.setGeometry(QtCore.QRect(570, 260, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
 self.menubar = QtWidgets.QMenuBar(MainWindow)
 self.menubar.setGeometry(QtCore.QRect(0, 0, 679, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
 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.label.setText(_translate("MainWindow", "Select a distribution for the arrival of customers :"))
 self.comboBox.setItemText(0, _translate("MainWindow", "Binomial"))
 self.comboBox.setItemText(1, _translate("MainWindow", "Geometric"))
 self.comboBox.setItemText(2, _translate("MainWindow", "Show"))
 self.label_2.setText(_translate("MainWindow", "Select number of banks :"))
 self.comboBox_2.setItemText(0, _translate("MainWindow", "1"))
 self.comboBox_2.setItemText(1, _translate("MainWindow", "2"))
 self.comboBox_2.setItemText(2, _translate("MainWindow", "3"))
 self.label_3.setText(_translate("MainWindow", "Specify the range of arrival(min) :"))
 self.label_4.setText(_translate("MainWindow", "From :"))
 self.label_5.setText(_translate("MainWindow", "To :"))
 self.pushButton.setText(_translate("MainWindow", "Proceed"))


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_())
March 19th 20 at 08:23
1 answer
March 19th 20 at 08:25
Solution
Use QStackedWidget
ui_main.py:

from PyQt5 import QtCore, QtGui, QtWidgets

import sys

class Ui_Main(QtWidgets.QWidget):
 def setupUi(self, Main):
Main.setObjectName("Main")
 Main.resize(800, 480)

 self.QtStack = QtWidgets.QStackedLayout()

 self.stack1 = QtWidgets.QWidget()
 self.stack2 = QtWidgets.QWidget()
 self.stack3 = QtWidgets.QWidget()

self.Window1UI()
self.Window2UI()
self.Window3UI()

self.QtStack.addWidget(self.stack1)
self.QtStack.addWidget(self.stack2)
self.QtStack.addWidget(self.stack3)

 def Window1UI(self):
 self.stack1.resize(800, 480)

#PushButton1#
 self.PushButton1 = QtWidgets.QPushButton(self.stack1)
 self.PushButton1.setText("BUTTON 1")
 self.PushButton1.setGeometry(QtCore.QRect(10, 10, 100, 100))

#PushButton2#
 self.PushButton2 = QtWidgets.QPushButton(self.stack1)
 self.PushButton2.setText("BUTTON 2")
 self.PushButton2.setGeometry(QtCore.QRect(150, 150, 100, 100))

 def Window2UI(self):
 self.stack2.resize(800, 480)
 self.stack2.setStyleSheet("background: red")

 def Window3UI(self):
 self.stack3.resize(800, 480)
 self.stack3.setStyleSheet("background: blue")

Main.py:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

import sys

from ui_Main import Ui_Main

class Main(QMainWindow, Ui_Main):
 def __init__(self, parent=None):
 super(Main, self).__init__(parent)
self.setupUi(self)

self.PushButton1.clicked.connect(self.OpenWindow1)
self.PushButton2.clicked.connect(self.OpenWindow2)

 def OpenWindow1(self):
self.QtStack.setCurrentIndex(1)

 def OpenWindow2(self):
self.QtStack.setCurrentIndex(2)

if __name__ == '__main__':
 app = QApplication(sys.argv)
 showMain = Main()
 sys.exit(app.exec_())

This "book" example, everything should be clear as day. All ui in a single file, their handler in the second. For the image to fit any functionality. He started it.
There's only one uncomfortable moment. When used in different Windows of the interface elements with the same name, they are overridden, this in different Windows does not work using the buttons with the same name (Button Button) need to use different names, that is Button_win1 and Button_win2 and the like. Also in the event handler signals need to be duplicated for each button, if they do the same thing. But, at the same time, different signals, you can perform the same function.
In example if you add
self.PushButton3.clicked.connect(self.OpenWindow2)
regularly will execute the same action that was performed with Button2.
It is the most inconvenient inconvenience. If we ignore the fact that all ui will be in one file and they can be "too much". When the desired bypass. - dewitt12 commented on March 19th 20 at 08:28

Find more questions by tags PyQtPython