当我们通过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

相关文章:

  • 2021-12-06
  • 2021-06-06
  • 2021-06-27
  • 2021-11-18
  • 2021-12-11
  • 2021-05-12
  • 2021-05-25
  • 2021-12-08
猜你喜欢
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案