网上关于PyQt5的教程很少,特别是界面跳转这一块儿,自己研究了半天,下来和大家分享一下

一、首先是主界面

 1 # -*- 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!
#要注意的是跳转界面第二个必须使用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转换成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): #这一块注意,是重点从主界面跳转到Demo1界面,主界面隐藏,如果关闭Demo界面,主界面进程会触发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_())

二、跳转界面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()

三、跳转界面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_())

最新文章

  1. 我所记录的git命令(非常实用)
  2. VS2013 添加已有文件夹
  3. QTableView 添加按钮
  4. hadoop之根据Rowkey从HBase中查询数据
  5. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
  6. 你可以使用 play framework 做5件很爽的事情http://www.anool.net/?p=629
  7. Python: 关于nose
  8. 11、网页制作Dreamweaver(补充:JS零碎知识点&&正则表达式)
  9. android webview如何加载asset目录里的页面
  10. bzoj1197
  11. linux 切换用户之后变成-bash-x.x$的解决方法
  12. hdu 1760 一道搜索博弈题 挺新颖的题目
  13. Android Studio | 详细安装教程
  14. java开源项目之IQQ学习记录之项目环境搭建与启动
  15. CSS解决字母不换行
  16. 【C++ Primer 第11章 练习答案】2. 关联容器操作
  17. 关于Tomcat配置虚拟路径保存、访问图片
  18. eclipse Maven 使用记录 ------ 建立app项目
  19. CRM系统新思维
  20. loj6436【PKUSC2018】神仙的游戏

热门文章

  1. (六十六)TableView内容超过一屏时滚动到屏幕底部的方法
  2. 在go中使用json作为主要的配置格式
  3. 11.3、Libgdx的音频之播放PCM音频
  4. Miscellaneous Articles
  5. (NO.00001)iOS游戏SpeedBoy Lite成形记(三)
  6. 【面试笔试算法】Program 2:Amusing Digits(网易游戏笔试题)
  7. RHEL自动安装zookeeper的shell脚本
  8. akamai:与看视频广告等待相比,用户更不能忍受缓冲等待
  9. LeetCode之“动态规划”:Word Break && Word Break II
  10. android:inputType常用取值