当我们通过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函数,即可查看效果。
主窗口:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 =