当我们通过pyqt开发时,eric6为我们提供了一个方便的工具:图形化的绘制UI工具--qtdesigner。我们可以通过它开发多个UI,然后利用信号-槽工具,将功能代码附着在上面。也可以将多个界面连接起来。接下来,我要提供将多个UI链接起来的思路。一:讲解:qtdesigner自动生成的代码是怎样运行的:(一)组成qtdesinger自动生成的代码为一个对象和对象启动命令,(如何将.ui文件生成.py文件查看:https://blog.csdn.net/qq_37193537/article/details/82080285)对象中包含两个函数,setupUi()和retranslateUi()。两个函数负责绘制Ui,其中setupUI会调用retranslateUi。
使用四个函数,一个主窗口,两个弹出窗口,一个主运行函数。对于所有的窗口都可以通过qtdesigner来设计,之后在主运行函数中来调用。直接运行run函数,即可查看效果。
主窗口:
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * ################################################ #######创建主窗口 ################################################ class FirstMainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle(‘主界面‘) ###### 创建界面 ###### self.centralwidget = QWidget() self.setCentralWidget(self.centralwidget) self.Layout = QVBoxLayout(self.centralwidget) # 设置顶部三个按钮 self.topwidget = QWidget() self.Layout.addWidget(self.topwidget) self.buttonLayout = QHBoxLayout(self.topwidget) self.pushButton1 = QPushButton() self.pushButton1.setText("打开主界面") self.buttonLayout.addWidget(self.pushButton1) self.pushButton2 = QPushButton() self.pushButton2.setText("打开对话框") self.buttonLayout.addWidget(self.pushButton2) self.pushButton3 = QPushButton() self.pushButton3.setText("打开提示框") self.buttonLayout.addWidget(self.pushButton3) # 设置中间文本 self.label = QLabel() self.label.setText("第一个主界面") self.label.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) self.label.setAlignment(Qt.AlignCenter) self.label.setFont(QFont("Roman times", 50, QFont.Bold)) self.Layout.addWidget(self.label) # 设置状态栏 self.statusBar().showMessage("当前用户:mxh") # 窗口最大化 self.showMaximized() ###### 三个按钮事件 ###### self.pushButton3.clicked.connect(self.on_pushButton3_clicked) # 按钮三:打开提示框 def on_pushButton3_clicked(self): QMessageBox.information(self, "提示", "这是information框!") #QMessageBox.question(self, "提示", "这是question框!") #QMessageBox.warning(self, "提示", "这是warning框!") #QMessageBox.about(self, "提示", "这是about框!") ################################################ #######程序入门 ################################################ if __name__ == "__main__": app = QApplication(sys.argv) the_mainwindow = FirstMainWindow() the_mainwindow.show() sys.exit(app.exec_())
main_window
第一个弹出窗口:
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog1(object): def setupUi1(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(480, 297) self.buttonBox = QtWidgets.QDialogButtonBox(Dialog) self.buttonBox.setGeometry(QtCore.QRect(260, 240, 181, 32)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") self.hostlnedt = QtWidgets.QLineEdit(Dialog) self.hostlnedt.setGeometry(QtCore.QRect(120, 20, 113, 27)) self.hostlnedt.setObjectName("hostlnedt") self.hostlbl = QtWidgets.QLabel(Dialog) self.hostlbl.setGeometry(QtCore.QRect(20, 26, 81, 21)) self.hostlbl.setObjectName("hostlbl") self.usernamelbl = QtWidgets.QLabel(Dialog) self.usernamelbl.setGeometry(QtCore.QRect(20, 90, 51, 17)) self.usernamelbl.setObjectName("usernamelbl") self.userlnedt = QtWidgets.QLineEdit(Dialog) self.userlnedt.setGeometry(QtCore.QRect(120, 80, 113, 27)) self.userlnedt.setObjectName("userlnedt") self.passwdlbl = QtWidgets.QLabel(Dialog) self.passwdlbl.setGeometry(QtCore.QRect(20, 140, 81, 20)) self.passwdlbl.setObjectName("passwdlbl") self.passwdlnedit = QtWidgets.QLineEdit(Dialog) self.passwdlnedit.setGeometry(QtCore.QRect(120, 130, 113, 27)) self.passwdlnedit.setObjectName("passwdlnedit") self.retranslateUi(Dialog) self.buttonBox.accepted.connect(Dialog.accept) self.buttonBox.rejected.connect(Dialog.reject) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "登陆mysql")) self.hostlnedt.setText(_translate("Dialog", "localhost")) self.hostlbl.setText(_translate("Dialog", "HOST地址")) self.usernamelbl.setText(_translate("Dialog", "用户名")) self.passwdlbl.setText(_translate("Dialog", "数据库密码")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog1 = QtWidgets.QDialog() ui = Ui_Dialog1() ui.setupUi1(Dialog1) Dialog1.show() sys.exit(app.exec_())
Ui_one
第二个弹出窗口:
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog2(object): def setupUi2(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 296) self.dialogButtonBox = QtWidgets.QDialogButtonBox(Dialog) self.dialogButtonBox.setGeometry(QtCore.QRect(190, 240, 181, 32)) self.dialogButtonBox.setOrientation(QtCore.Qt.Horizontal) self.dialogButtonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.dialogButtonBox.setObjectName("dialogButtonBox") self.author = QtWidgets.QLabel(Dialog) self.author.setGeometry(QtCore.QRect(40, 20, 191, 17)) self.author.setObjectName("author") self.email = QtWidgets.QLabel(Dialog) self.email.setGeometry(QtCore.QRect(40, 50, 271, 17)) self.email.setObjectName("email") self.github = QtWidgets.QLabel(Dialog) self.github.setGeometry(QtCore.QRect(40, 80, 351, 17)) self.github.setObjectName("github") self.GPL = QtWidgets.QLabel(Dialog) self.GPL.setGeometry(QtCore.QRect(40, 110, 161, 17)) self.GPL.setObjectName("GPL") self.retranslateUi(Dialog) self.dialogButtonBox.accepted.connect(Dialog.accept) self.dialogButtonBox.rejected.connect(Dialog.reject) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.author.setText(_translate("Dialog", "作者:maxiaohui")) self.email.setText(_translate("Dialog", "联系邮箱:[email protected]")) self.github.setText(_translate("Dialog", "github: https://github.com/xxx")) self.GPL.setText(_translate("Dialog", "本软件遵循GPL v2协议")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog2 = QtWidgets.QDialog() ui2 = Ui_Dialog2() ui2.setupUi2(Dialog2) Dialog2.show() sys.exit(app.exec_())
Ui_two
主运行函数:
# coding:utf-8 import sys from PyQt5 import QtCore, QtGui, QtWidgets from pyqt5_work.GUI_more.main_window import FirstMainWindow from pyqt5_work.GUI_more.Ui_one import Ui_Dialog1 from pyqt5_work.GUI_more.Ui_two import Ui_Dialog2 # part 1 # 实例化启动qt应用 app = QtWidgets.QApplication(sys.argv) # 取得UI class 的实例 the_mainwindow = FirstMainWindow() # part 2 登录mysql Dialog1 = QtWidgets.QDialog() ui1 = Ui_Dialog1() ui1.setupUi1(Dialog1) # part 3 关于界面 Dialog2 = QtWidgets.QDialog() ui2 = Ui_Dialog2() ui2.setupUi2(Dialog2) # part 4 # 设计信号-槽点 # 建立对象实例化的访问 bttn = the_mainwindow.pushButton1 bttn.clicked.connect(Dialog1.show) bttn2 = the_mainwindow.pushButton2 bttn2.clicked.connect(Dialog2.show) if __name__ == "__main__": # 展示主窗口 chenSpider the_mainwindow.show() sys.exit(app.exec_())
run
另外还查看了另外一种方式,所有的窗口和函数都在一个函数当中(函数直接运行即可查看效果)
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * ################################################ #######创建主窗口 ################################################ class FirstMainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle(‘主界面‘) ###### 创建界面 ###### self.centralwidget = QWidget() self.setCentralWidget(self.centralwidget) self.Layout = QVBoxLayout(self.centralwidget) # 设置顶部三个按钮 self.topwidget = QWidget() self.Layout.addWidget(self.topwidget) self.buttonLayout = QHBoxLayout(self.topwidget) self.pushButton1 = QPushButton() self.pushButton1.setText("打开主界面") self.buttonLayout.addWidget(self.pushButton1) self.pushButton2 = QPushButton() self.pushButton2.setText("打开对话框") self.buttonLayout.addWidget(self.pushButton2) self.pushButton3 = QPushButton() self.pushButton3.setText("打开提示框") self.buttonLayout.addWidget(self.pushButton3) # 设置中间文本 self.label = QLabel() self.label.setText("第一个主界面") self.label.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) self.label.setAlignment(Qt.AlignCenter) self.label.setFont(QFont("Roman times", 50, QFont.Bold)) self.Layout.addWidget(self.label) # 设置状态栏 self.statusBar().showMessage("当前用户:一心狮") # 窗口最大化 self.showMaximized() ###### 三个按钮事件 ###### self.pushButton1.clicked.connect(self.on_pushButton1_clicked) self.pushButton2.clicked.connect(self.on_pushButton2_clicked) self.pushButton3.clicked.connect(self.on_pushButton3_clicked) # 按钮一:打开主界面 windowList = [] def on_pushButton1_clicked(self): the_window =SecondWindow() self.windowList.append(the_window) ##注:没有这句,是不打开另一个主界面的! self.close() the_window.show() # 按钮二:打开对话框 def on_pushButton2_clicked(self): the_dialog = demoDialog_t() if the_dialog.exec_() == QDialog.Accepted: pass # 按钮三:打开提示框 def on_pushButton3_clicked(self): QMessageBox.information(self, "提示", "这是information框!") #QMessageBox.question(self, "提示", "这是question框!") #QMessageBox.warning(self, "提示", "这是warning框!") #QMessageBox.about(self, "提示", "这是about框!") ################################################ #######第二个主界面 ################################################ class SecondWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle(‘第二主界面‘) # 设置中间文本 self.label = QLabel() self.label.setText("第二个主界面") self.label.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) self.label.setAlignment(Qt.AlignCenter) self.label.setFont(QFont("Roman times", 50, QFont.Bold)) self.setCentralWidget(self.label) # 设置状态栏 self.statusBar().showMessage("当前用户:一心狮") # 窗口最大化 self.showMaximized() ###### 重写关闭事件,回到第一界面 windowList = [] def closeEvent(self, event): the_window = FirstMainWindow() self.windowList.append(the_window) ##注:没有这句,是不打开另一个主界面的! the_window.show() event.accept() ################################################ #######对话框 ################################################ class demoDialog_t(QDialog): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle(‘对话框‘) ### 设置对话框类型 self.setWindowFlags(Qt.Tool) ################################################ #######程序入门 ################################################ if __name__ == "__main__": app = QApplication(sys.argv) the_mainwindow = FirstMainWindow() the_mainwindow.show() sys.exit(app.exec_())
参考:
https://www.aliyun.com/jiaocheng/522668.html
https://blog.csdn.net/qq_20265805/article/details/82153143
原文地址:https://www.cnblogs.com/mxhmxh/p/9803354.html
时间: 2024-11-09 14:43:27