PyQt5实现界面跳转
网上关于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_())
PyQt5实现界面跳转的更多相关文章
- PYQT5登录界面跳转主界面方法
该问题,有很多种方法,但是很多方法要么这个有问题,要么那个有问题,最后终于找到一种没问题的方法.记录一下: 参考地址:https://www.jianshu.com/p/d18ff36a78d6?fr ...
- 完美解决PYQT5登录界面跳转主界面方法
该问题,有很多种方法,但是很多方法要么这个有问题,要么那个有问题,最后终于找到一种没问题的方法.记录一下: Login.py(登录窗口)文件 import sys from PyQt5 import ...
- Android 手机卫士--设置界面&功能列表界面跳转逻辑处理
在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...
- iOS界面跳转的一些优化方案
原文地址: http://blog.startry.com/2016/02/14/Think-Of-UIViewController-Switch/ iOS界面跳转的一些优化方案 App应用程序开发, ...
- Android activity界面跳转动画
实现activity界面跳转动画 1.在startActivity方法之后加入: overridePendingTransition(R.anim.pull_in_right, R.anim.pull ...
- 如何优雅的实现界面跳转 之 统跳协议 - DarwinNativeRouter
PS 感谢大家的关注,由于我本想开源4个库,除了router, 另外三个分别是native dispatcher, web dispatcher 和 react dispatcher , 所以rout ...
- ios中的界面跳转方式
ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...
- Android原生和H5交互;Android和H5混合开发;WebView点击H5界面跳转到Android原生界面。
当时业务的需求是这样的,H5有一个活动商品列表的界面,IOS和Android共用这一个界面,点击商品可以跳转到Android原生的商品详情界面并传递商品ID: 大概就是点击H5界面跳转到Androi ...
- ios的两种界面跳转方式
1.在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面,使用这种方式无法实现界面跳转时的动画效果. if(self.rootViewController.view.supervie ...
随机推荐
- UNIX环境高级编程——管道和FIFO限制
系统加于管道和FIFO的唯一限制为: OPEN_MAX 一个进程在任意时刻打开的最大描述符数: PIPE_BUF 可原子的写往一个管道或FIFO的最大数据量. OPEN_MAX的值 ...
- 分布式进阶(二)Ubuntu 14.04下安装Dockr图文教程(一)
当前,完全硬件虚拟化技术(KVM.Xen.Hyper-V 等)能在一个物理主机上很好地运行多个互相独立的操作系统,但这也带来一些问题:性能不佳,资源浪费,系统反应迟缓等.有时候对用户来说,完全的硬件虚 ...
- Android-获取全局Context的技巧-android学习之旅(68)
我们经常需要获取全局的Context ,比如弹出Toast,启动活动,服务,接收器,还有自定义控件,操作数据库,使用通知等 通常的方法是在调用的地方传入Context参数 ,有时候这种不会奏效,教给大 ...
- Java进阶(二十六)公司项目开发知识点回顾
公司项目开发知识点回顾 前言 "拿来主义"在某些时候并不是最佳选择,尤其是当自己遇到问题的时候,毫无头绪. 在一次实验过程中,需要实现数据库的CRUD操作.由于之前项目开发过程中, ...
- Leetcode_21_Merge Two Sorted Lists
->4->4,return 1->2->3->4->5->6. 思路: (1)题意为将两个有序链表合成一个有序链表. (2)首先,分别对链表头结点判空,如果都 ...
- mongoDB基本使用(二)
数据库基本操作 连接到mongoDB服务器 ./bin/mongo 127.0.0.1:12345 查看当前数据库 > show dbs admin (empty) local 0.078 ...
- Java 对象在堆中的内存结构
翻译人员: 铁锚 翻译日期: 2013年11月8日 原文链接: What do Java objects look like in memory during run-time? 我们知道,函数每次 ...
- (四十七)Quartz2D引擎初步
Quartz2D是跨平台的,同时支持iOS与Mac. 支持圆型裁剪,可以实现圆形头像等功能,也支持手势解锁.折线图等的制作. 对于复杂的UI界面,还可以通过Quartz2D将控件内部的结构画出来,可用 ...
- 详解EBS接口开发之物料导入API
create_item inv_item_grp.create_item(p_commit => fnd_api.g_true, -- p_item_rec => l_item_rec, ...
- 【一天一道LeetCode】#46. Permutations
一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...