PyQt5實現(xiàn)界面(頁面)跳轉(zhuǎn)的示例代碼
更新時間:2021年04月07日 10:32:13 作者:Awzh
這篇文章主要介紹了PyQt5實現(xiàn)界面跳轉(zhuǎn)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
網(wǎng)上關于PyQt5的教程很少,特別是界面跳轉(zhuǎn)這一塊兒,自己研究了半天,下來和大家分享一下
一、首先是主界面
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'Form.ui' # # Created by: PyQt5 UI code generator 5.10.1 # # WARNING! All changes made in this file will be lost! #要注意的是跳轉(zhuǎn)界面第二個必須使用QDialog類,不能使用QWidget,我也不知道為什么,特別注意 from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QApplication import Dialog1 import Dialog2 import sys class Ui_Form(object): #這是用PyQt Designer生成的代碼,很簡單的,拖動控件,生成ui文件,然后UIC轉(zhuǎn)換成py文件 def setupUi(self, Form): Form.setObjectName("Form") Form.resize(440, 310) self.form = Form self.btn_d1 = QtWidgets.QPushButton(Form) self.btn_d1.setGeometry(QtCore.QRect(60, 140, 75, 23)) self.btn_d1.setObjectName("btn_d1") self.btn_d2 = QtWidgets.QPushButton(Form) self.btn_d2.setGeometry(QtCore.QRect(180, 140, 75, 23)) self.btn_d2.setObjectName("btn_d2") self.btn_exit = QtWidgets.QPushButton(Form) self.btn_exit.setGeometry(QtCore.QRect(310, 140, 75, 23)) self.btn_exit.setObjectName("btn_exit") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.btn_d1.setText(_translate("Form", "Demo1")) self.btn_d1.clicked.connect(self.jump_to_demo1) self.btn_d2.setText(_translate("Form", "Demo2")) self.btn_d2.clicked.connect(self.jump_to_demo2) self.btn_exit.setText(_translate("Form", "Exit")) self.btn_exit.clicked.connect(self.exit) def jump_to_demo1(self): #這一塊注意,是重點從主界面跳轉(zhuǎn)到Demo1界面,主界面隱藏,如果關閉Demo界面,主界面進程會觸發(fā)self.form.show()會再次顯示主界面 self.form.hide() #如果沒有self.form.show()這一句,關閉Demo1界面后就會關閉程序 form1 = QtWidgets.QDialog() ui = Dialog1.Ui_Dialog1() ui.setupUi(form1) form1.show() form1.exec_() self.form.show() def jump_to_demo2(self): self.form.hide() form2 = QtWidgets.QDialog() ui = Dialog2.Ui_Dialog2() ui.setupUi(form2) form2.show() form2.exec_() self.form.show() def exit(self): self.form.close() if __name__ == "__main__": app = QApplication(sys.argv) form = QtWidgets.QWidget() window = Ui_Form() window.setupUi(form) form.show() sys.exit(app.exec_())
二、跳轉(zhuǎn)界面Demo1
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'Dialog1.ui' # # Created by: PyQt5 UI code generator 5.10.1 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog1(object): def setupUi(self, Dialog1): Dialog1.setObjectName("Dialog1") Dialog1.resize(400, 300) self.dialog=Dialog1 self.pushButton = QtWidgets.QPushButton(Dialog1) self.pushButton.setGeometry(QtCore.QRect(140, 140, 75, 23)) self.pushButton.setObjectName("pushButton") self.retranslateUi(Dialog1) QtCore.QMetaObject.connectSlotsByName(Dialog1) def retranslateUi(self, Dialog1): _translate = QtCore.QCoreApplication.translate Dialog1.setWindowTitle(_translate("Dialog1", "Dialog")) self.pushButton.setText(_translate("Dialog1", "Jump to main")) self.pushButton.clicked.connect(self.jump_to_main) def jump_to_main(self): self.dialog.close()
三、跳轉(zhuǎn)界面Demo2
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'Dialog2.ui' # # Created by: PyQt5 UI code generator 5.10.1 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication import sys class Ui_Dialog2(object): def setupUi(self, Dialog2): Dialog2.setObjectName("Dialog2") Dialog2.resize(400, 300) self.dialog = Dialog2 self.pushButton = QtWidgets.QPushButton(Dialog2) self.pushButton.setGeometry(QtCore.QRect(140, 160, 75, 23)) self.pushButton.setObjectName("pushButton") self.retranslateUi(Dialog2) QtCore.QMetaObject.connectSlotsByName(Dialog2) def retranslateUi(self, Dialog2): _translate = QtCore.QCoreApplication.translate Dialog2.setWindowTitle(_translate("Dialog2", "Dialog")) self.pushButton.setText(_translate("Dialog2", "Jump to main")) self.pushButton.clicked.connect(self.go_main) def go_main(self): self.dialog.close() if __name__ == "__main__": app = QApplication(sys.argv) form = QtWidgets.QDialog() ui = Ui_Dialog2() ui.setupUi(form) form.show() sys.exit(app.exec_())
到此這篇關于PyQt5實現(xiàn)界面跳轉(zhuǎn)的示例代碼的文章就介紹到這了,更多相關PyQt5 界面跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ROS1?rosbag的詳細使用并且使用python合并bag包的方法
這篇文章主要介紹了ROS1?rosbag的詳細使用,并且使用python來合并bag包,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05python報錯TypeError: ‘NoneType‘ object is not subscriptable的解決
這篇文章主要給大家介紹了關于python報錯TypeError: ‘NoneType‘ object is not subscriptable的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11Django+JS 實現(xiàn)點擊頭像即可更改頭像的方法示例
這篇文章主要介紹了Django+JS 實現(xiàn)點擊頭像即可更改頭像的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12