這篇文章給大家介紹使用PYQT5怎么實(shí)現(xiàn)一個(gè)界面嵌套功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
為運(yùn)河等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及運(yùn)河網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、運(yùn)河網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!簡單演示下:
首先主界面界面:
在Form處設(shè)置下水平布局,就如下圖:
左右兩側(cè)分別是一個(gè)frame控件,左側(cè)frame里含有兩個(gè)按鈕,右側(cè)其實(shí)我只是用來占位的,后面解釋。選中兩個(gè)frame,點(diǎn)擊 Lay out Horizontally in Splitter,將它設(shè)置為 Splitter 布局,為了能讓要切換的界面在右側(cè)的 frame 顯示出來。
接下來是界面1:
界面2:
將三個(gè) .ui文件轉(zhuǎn)為 .py 文件,然后在代碼中調(diào)整。
在index.py文件下導(dǎo)入 first 和 second
from first import Ui_First from second import Ui_Second
在index.py文件下生成 first 和 second 兩個(gè)子類:
class First(QWidget, Ui_First): def __init__(self): super(First,self).__init__() # 子窗口初始化時(shí)實(shí)現(xiàn)子窗口布局 self.setupUi(self) # 設(shè)置子窗體最小尺寸 self.setMinimumWidth(30) self.setMinimumHeight(30) class Second(QWidget, Ui_Second): def __init__(self): super(Second,self).__init__() self.setupUi(self) self.setMinimumWidth(30) self.setMinimumHeight(30)
在index.py的初始化里面生成兩個(gè)界面的對象
# 初始化兩個(gè)對象,并把 first對象 加入到 splitter 中 self.first = First() self.second = Second() self.splitter.addWidget(self.first)
這個(gè)時(shí)候運(yùn)行可能會報(bào)錯(cuò):
Traceback (most recent call last):
File "D:/python/workpace/Normal/cad/index.py", line 93, in <module>
ui.setupUi(MainWindow)
File "D:/python/workpace/Normal/cad/index.py", line 60, in setupUi
self.splitter.addWidget(self.first)
TypeError: addWidget(self, QWidget): argument 1 has unexpected type 'Ui_First'
原因是在 first.py 這個(gè)子界面中,是沒有菜單欄等這些主界面才有的界面,因?yàn)橹苯佑?qt 生成的 ui 文件轉(zhuǎn)化的,所以代碼還是帶有菜單欄這些的,我們只要注釋掉,并且把self.centralwidget換成 對象名即可。(同樣 second.py 也要)
最后只要綁定按鈕事件
self.pushButton.clicked.connect(lambda :self.change(self.pushButton.objectName())) self.pushButton_2.clicked.connect(lambda :self.change(self.pushButton_2.objectName())) def change(self,name): if name == "pushButton": # 這句的意思是把現(xiàn)在 splitter 的界面設(shè)置為None,也就是去掉 self.splitter.widget(1).setParent(None) # 把另一個(gè)self.first這個(gè)界面插入上一句去掉的那個(gè)界面的位置 self.splitter.insertWidget(1, self.first) if name == "pushButton_2": self.splitter.widget(1).setParent(None) self.splitter.insertWidget(1, self.second)
這樣運(yùn)行,就大功告成啦
index.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'index.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QWidget from first import Ui_First from second import Ui_Second 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.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget) self.horizontalLayout.setObjectName("horizontalLayout") self.splitter = QtWidgets.QSplitter(self.centralwidget) self.splitter.setOrientation(QtCore.Qt.Horizontal) self.splitter.setObjectName("splitter") self.frame = QtWidgets.QFrame(self.splitter) self.frame.setMaximumSize(QtCore.QSize(200, 16777215)) self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel) self.frame.setFrameShadow(QtWidgets.QFrame.Raised) self.frame.setObjectName("frame") self.verticalLayout = QtWidgets.QVBoxLayout(self.frame) self.verticalLayout.setObjectName("verticalLayout") self.pushButton = QtWidgets.QPushButton(self.frame) self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.pushButton_2 = QtWidgets.QPushButton(self.frame) self.pushButton_2.setObjectName("pushButton_2") self.verticalLayout.addWidget(self.pushButton_2) # 這里注釋掉,因?yàn)椴恍枰猣rame2。當(dāng)初在qt 里面設(shè)計(jì)這個(gè)frame2的原因是為了占位,不至于到時(shí)候布局出現(xiàn)錯(cuò)亂。 # self.frame_2 = QtWidgets.QFrame(self.splitter) # self.frame_2.setStyleSheet("background-color:white;") # self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel) # self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised) # self.frame_2.setObjectName("frame_2") self.horizontalLayout.addWidget(self.splitter) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) # 初始化兩個(gè)對象,并把 first對象 加入到 splitter 中 self.first = First() self.second = Second() self.splitter.addWidget(self.first) self.pushButton.clicked.connect(lambda :self.change(self.pushButton.objectName())) self.pushButton_2.clicked.connect(lambda :self.change(self.pushButton_2.objectName())) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "界面1")) self.pushButton_2.setText(_translate("MainWindow", "界面2")) def change(self,name): if name == "pushButton": self.splitter.widget(1).setParent(None) self.splitter.insertWidget(1, self.first) if name == "pushButton_2": self.splitter.widget(1).setParent(None) self.splitter.insertWidget(1, self.second) class First(QWidget, Ui_First): def __init__(self): super(First,self).__init__() # 子窗口初始化時(shí)實(shí)現(xiàn)子窗口布局 self.setupUi(self) # 設(shè)置子窗體最小尺寸 self.setMinimumWidth(30) self.setMinimumHeight(30) class Second(QWidget, Ui_Second): def __init__(self): super(Second,self).__init__() self.setupUi(self) self.setMinimumWidth(30) self.setMinimumHeight(30) if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
first.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'first.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_First(object): def setupUi(self, Ui_First): Ui_First.setObjectName("MainWindow") Ui_First.resize(800, 600) # self.centralwidget = QtWidgets.QWidget(Ui_First) # self.centralwidget.setObjectName("centralwidget") self.horizontalLayout = QtWidgets.QHBoxLayout(Ui_First) self.horizontalLayout.setObjectName("horizontalLayout") self.frame_2 = QtWidgets.QFrame(Ui_First) self.frame_2.setStyleSheet("background-color:white;") self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel) self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised) self.frame_2.setObjectName("frame_2") self.label = QtWidgets.QLabel(self.frame_2) self.label.setGeometry(QtCore.QRect(300, 180, 181, 81)) font = QtGui.QFont() font.setPointSize(20) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.frame_2) # Ui_First.setCentralWidget(self.centralwidget) # self.menubar = QtWidgets.QMenuBar(Ui_First) # self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) # self.menubar.setObjectName("menubar") # Ui_First.setMenuBar(self.menubar) # self.statusbar = QtWidgets.QStatusBar(Ui_First) # self.statusbar.setObjectName("statusbar") # Ui_First.setStatusBar(self.statusbar) self.retranslateUi(Ui_First) QtCore.QMetaObject.connectSlotsByName(Ui_First) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "這是界面1"))
second.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'second.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Second(object): def setupUi(self, Ui_Second): Ui_Second.setObjectName("MainWindow") Ui_Second.resize(800, 600) # self.centralwidget = QtWidgets.QWidget(Ui_Second) # self.centralwidget.setObjectName("centralwidget") self.horizontalLayout = QtWidgets.QHBoxLayout(Ui_Second) self.horizontalLayout.setObjectName("horizontalLayout") self.frame_2 = QtWidgets.QFrame(Ui_Second) self.frame_2.setStyleSheet("background-color:green;") self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel) self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised) self.frame_2.setObjectName("frame_2") self.label = QtWidgets.QLabel(self.frame_2) self.label.setGeometry(QtCore.QRect(300, 180, 181, 81)) font = QtGui.QFont() font.setPointSize(20) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.frame_2) # Ui_Second.setCentralWidget(self.centralwidget) # self.menubar = QtWidgets.QMenuBar(Ui_Second) # self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) # self.menubar.setObjectName("menubar") # Ui_Second.setMenuBar(self.menubar) # self.statusbar = QtWidgets.QStatusBar(Ui_Second) # self.statusbar.setObjectName("statusbar") # Ui_Second.setStatusBar(self.statusbar) self.retranslateUi(Ui_Second) QtCore.QMetaObject.connectSlotsByName(Ui_Second) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "這是界面2"))
關(guān)于使用PYQT5怎么實(shí)現(xiàn)一個(gè)界面嵌套功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章題目:使用PYQT5怎么實(shí)現(xiàn)一個(gè)界面嵌套功能-創(chuàng)新互聯(lián)
文章路徑:http://aaarwkj.com/article46/cojheg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站維護(hù)、網(wǎng)站營銷、企業(yè)網(wǎng)站制作、品牌網(wǎng)站制作、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)