纯代码:
新建个工程,下面新建三个文件:
one.py
ui_denglu.py
ui_zhuche.py
三个文件内代码如下:
# one.py文件代码
import ui_denglu as dl
import ui_zhuche as zc
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
import sys
class parentWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = dl.Ui_submit_user()
self.main_ui.setupUi(self)
class childWindow(QDialog):
def __init__(self):
QDialog.__init__(self)
self.child = zc.Ui_MainWindow()
self.child.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = parentWindow()
child = childWindow()
# 通过toolButton将两个窗体关联
btn = window.main_ui.Ui_submit_user
btn.clicked.connect(child.show)
# 显示
window.show()
sys.exit(app.exec_())
# ui_denglu.py文件代码
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_submit_user(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(140, 120, 101, 71))
self.label.setObjectName("label")
self.Ui_submit_user = QtWidgets.QPushButton(Form)
self.Ui_submit_user.setGeometry(QtCore.QRect(130, 190, 93, 28))
self.Ui_submit_user.setObjectName("Ui_submit_user")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "我是登陆"))
self.Ui_submit_user.setText(_translate("Form", "注册页面"))
# ui_zhuche.py文件代码
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(150, 170, 72, 15))
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "我是注册"))
运行one.py.出现主界面,点击按钮,出现第二个窗口
设计界面操作
打开QT Designer设计两个如图所示的界面
类别可以到后面改
再pycharm中重构它,把.ui文件变成.py文件
ui_denglu文件中,把class改为Ui_submit_user
ui_zhuce同理,改为Ui_MainWindow
要注意这里的按钮名称要一致
运行即可