Сначала я создал MainWindow в Qt Designer
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QCursor
from reg import Ui_Form
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setFixedSize(660, 444)
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
MainWindow.setFont(font)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setStyleSheet("background: url(\'C:/Users/user/PycharmProjects/restaurant/media/1_img_rest.jpg\')")
self.centralwidget.setObjectName("centralwidget")
self.pushButton_entry = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_entry.setGeometry(QtCore.QRect(210, 250, 234, 51))
self.pushButton_entry.setMinimumSize(QtCore.QSize(230, 51))
self.pushButton_entry.setMaximumSize(QtCore.QSize(16777215, 51))
font = QtGui.QFont()
font.setPointSize(-1)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.pushButton_entry.setFont(font)
self.pushButton_entry.setStyleSheet("border-style: outset;\n"
" border-width: 3px;\n"
" border-radius: 15px;\n"
" border-color: beige;\n"
" font: bold 14px;\n"
" min-width: 12em;\n"
" padding: 10px;\n"
" background: url(\'C:/Users/user/Downloads/Screenshot.png\'); ")
self.pushButton_entry.setObjectName("pushButton_entry")
self.pushButton_reg = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_reg.setGeometry(QtCore.QRect(230, 310, 191, 41))
self.pushButton_reg.setStyleSheet("border-style: outset;\n"
" border-width: 3px;\n"
" border-radius: 15px;\n"
" border-color: beige;\n"
" font: bold 14px;\n"
" min-width: 8em;\n"
" padding: 10px;\n"
" background: url(\'C:/Users/user/Downloads/Screenshot.png\'); ")
self.pushButton_entry.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton_reg.setObjectName("pushButton_reg")
self.pushButton_reg.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(290, 190, 131, 21))
self.lineEdit.setStyleSheet("background: transparent;\n"
"background-color: rgb(255, 255, 255);")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(290, 220, 131, 21))
self.lineEdit_2.setStyleSheet("background: transparent;\n"
"background-color: rgb(255, 255, 255);")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(240, 190, 51, 21))
self.label.setStyleSheet("background: transparent;\n"
"background-color: rgb(255, 255, 255);")
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(240, 220, 51, 21))
self.label_2.setStyleSheet("background: transparent;\n"
"background-color: rgb(255, 255, 255);")
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.add_function()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Ресторан"))
self.pushButton_entry.setText(_translate("MainWindow", "Вход"))
self.pushButton_reg.setText(_translate("MainWindow", "Регистрация"))
self.label.setText(_translate("MainWindow", "Логин"))
self.label_2.setText(_translate("MainWindow", "Пароль"))
def add_function(self):
self.pushButton_reg.clicked.connect(self.create_reg_window)
def create_reg_window(self):
app = QtWidgets.QApplication(sys.argv)
self.Form = QtWidgets.QWidget()
self.ui = Ui_Form()
self.ui.setupUi(self.Form)
self.Form.show()
sys.exit(app.exec_())
Затем создал Widget окно, которое должно открываться после нажатия на кнопку "Регистрация" в основном окне. Функция отвечающая за это:
def create_reg_window(self):
app = QtWidgets.QApplication(sys.argv)
self.Form = QtWidgets.QWidget()
self.ui = Ui_Form()
self.ui.setupUi(self.Form)
self.Form.show()
sys.exit(app.exec_())
Весь код окна регистрации:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QCursor
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(251, 187)
Form.setStyleSheet("background-color: rgb(85, 85, 127);")
self.layoutWidget = QtWidgets.QWidget(Form)
self.layoutWidget.setGeometry(QtCore.QRect(40, 30, 188, 94))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_reg_name = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.label_reg_name.setFont(font)
self.label_reg_name.setObjectName("label_reg_name")
self.verticalLayout_2.addWidget(self.label_reg_name)
self.label_reg_login = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.label_reg_login.setFont(font)
self.label_reg_login.setObjectName("label_reg_login")
self.verticalLayout_2.addWidget(self.label_reg_login)
self.label_reg_password = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.label_reg_password.setFont(font)
self.label_reg_password.setObjectName("label_reg_password")
self.verticalLayout_2.addWidget(self.label_reg_password)
self.gridLayout.addLayout(self.verticalLayout_2, 0, 0, 1, 1)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit_reg_name = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_reg_name.setStyleSheet("border-style: outset;\n"
" border-width: 5px;\n"
" border-radius: 10px;\n"
" border-color: beige;")
self.lineEdit_reg_name.setObjectName("lineEdit_reg_name")
self.verticalLayout.addWidget(self.lineEdit_reg_name)
self.lineEdit_reg_login = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_reg_login.setStyleSheet("border-style: outset;\n"
" border-width: 5px;\n"
" border-radius: 10px;\n"
" border-color: beige;")
self.lineEdit_reg_login.setObjectName("lineEdit_reg_login")
self.verticalLayout.addWidget(self.lineEdit_reg_login)
self.lineEdit_reg_password = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_reg_password.setStyleSheet("border-style: outset;\n"
" border-width: 5px;\n"
" border-radius: 10px;\n"
" border-color: beige;")
self.lineEdit_reg_password.setObjectName("lineEdit_reg_password")
self.verticalLayout.addWidget(self.lineEdit_reg_password)
self.gridLayout.addLayout(self.verticalLayout, 0, 1, 1, 1)
self.pushButton_reg_reg = QtWidgets.QPushButton(Form)
self.pushButton_reg_reg.setGeometry(QtCore.QRect(50, 130, 170, 41))
self.pushButton_reg_reg.setStyleSheet("background-color: rgb(85, 85, 127);\n"
"border-style: outset;\n"
" border-width: 5px;\n"
" border-radius: 10px;\n"
" border-color: beige;\n"
" font: bold 11px;\n"
" min-width: 7em;\n"
" padding: 10px;")
self.pushButton_reg_reg.setObjectName("pushButton_reg_reg")
self.pushButton_reg_reg.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label_reg_name.setText(_translate("Form", "Имя"))
self.label_reg_login.setText(_translate("Form", "Логин"))
self.label_reg_password.setText(_translate("Form", "Пароль"))
self.pushButton_reg_reg.setText(_translate("Form", "Зарегестрироваться"))
После нажатия на кнопку регистрации в основном окне, окно регистрации открывается на коротки период, а затем программа выключается