欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python qt5编写第三个gui---由主窗口弹出子窗

程序员文章站 2022-03-13 17:24:55
Python qt5由主窗口弹出子窗在主窗口有几个按钮,通过按按钮,可以弹出来子窗口,完成效果如下图。1.首先将主窗口的ui文件转化为Python文件主窗口文件Ui_jiemian3.py# -*- coding: utf-8 -*-# Form implementation generated from reading ui file '/home/li/桌面/python/qt/jiemiansan/jiemian3.ui'## Created by: PyQt5 UI code g...

Python qt5编写第三个gui—由主窗口弹出子窗

在主窗口有几个按钮,通过按按钮,可以弹出来子窗口,完成效果如下图。
Python qt5编写第三个gui---由主窗口弹出子窗

1.首先将主窗口的ui文件转化为Python文件

主窗口文件

Ui_jiemian3.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/home/li/桌面/python/qt/jiemiansan/jiemian3.ui'
#
# Created by: PyQt5 UI code generator 5.12
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.changgui = QtWidgets.QPushButton(self.centralwidget)
        self.changgui.setGeometry(QtCore.QRect(560, 90, 141, 25))
        self.changgui.setObjectName("changgui")
        self.zhineng = QtWidgets.QPushButton(self.centralwidget)
        self.zhineng.setGeometry(QtCore.QRect(560, 200, 141, 25))
        self.zhineng.setObjectName("zhineng")
        self.jiekou = QtWidgets.QPushButton(self.centralwidget)
        self.jiekou.setGeometry(QtCore.QRect(560, 300, 141, 25))
        self.jiekou.setObjectName("jiekou")
        self.yingyongzhanwang = QtWidgets.QPushButton(self.centralwidget)
        self.yingyongzhanwang.setGeometry(QtCore.QRect(560, 400, 141, 25))
        self.yingyongzhanwang.setObjectName("yingyongzhanwang")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(90, 210, 381, 91))
        font = QtGui.QFont()
        font.setPointSize(30)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.tuichu = QtWidgets.QPushButton(self.centralwidget)
        self.tuichu.setGeometry(QtCore.QRect(670, 490, 89, 25))
        self.tuichu.setObjectName("tuichu")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.changgui.setText(_translate("MainWindow", "常规控制技术"))
        self.zhineng.setText(_translate("MainWindow", "智能控制技术"))
        self.jiekou.setText(_translate("MainWindow", "接口技术"))
        self.yingyongzhanwang.setText(_translate("MainWindow", "应用展望"))
        self.label.setText(_translate("MainWindow", "计算机接口控制技术"))
        self.tuichu.setText(_translate("MainWindow", "退出"))

子窗口文件

Ui_zijiemian.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/home/li/桌面/python/qt/jiemiansan/zijiemian.ui'
#
# Created by: PyQt5 UI code generator 5.12
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(617, 395)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(230, 10, 211, 41))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName("label")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "常规控制技术"))

创建调用文件

创建调用文件.py

# -*- coding: utf-8 -*-

import sys
import cv2
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
from Ui_jiemian3 import Ui_MainWindow
from Ui_zijiemian import Ui_Dialog
import os

class Ui_Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self):
        super(Ui_Dialog, self).__init__()
        self.setupUi(self)

class MainWindow(QMainWindow, Ui_MainWindow):
    
    def __init__(self, parent=None):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        # 设置【打开子窗口】按钮
        self.changgui.clicked.connect(self.showDialog)
 
    def showDialog(self):
        # 创建子窗口实例
        dialog = Ui_Dialog()
        # 显示子窗口
        dialog.show()
        # 实现子窗口中的【确定】按钮功能
        # 关联【确定】按钮
        dialog.exec_()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

首先调入俩个文件的类,然后再创建一个主窗口实例和子窗口实例,
在主窗口的类中定义打开子窗口的函数showDialog

参考链接:

pyqt:点击按键打开新/子窗口的两种方法

python pyqt5 点击按钮来打开另一个窗口

【PyQt】点击一个窗口的按钮来打开另一个窗口

本文地址:https://blog.csdn.net/weixin_45839124/article/details/107225100