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 ...
随机推荐
- 1022. Digital Library (30) -map -字符串处理
题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key w ...
- 调试bootmgr&winload vista&win7 x86&x64
设置调试bootmgr 1.以管理员权限运行cmd.exe 2.执行以下命令 3. 参照我的另一篇文章<win8 + vmware + windbg 双机调试 >中的第1.3步,建立wi ...
- Chapter 2 User Authentication, Authorization, and Security(11):在已还原的数据库中修正登录映射错误
原文出处:http://blog.csdn.net/dba_huangzj/article/details/39496517,专题目录:http://blog.csdn.net/dba_huangzj ...
- C语言实现的猜数字小游戏(主要是对于自定义函数的运用)
#include <stdio.h> #include <stdlib.h> #include<time.h>//加上此头文件的作用是什么?另外不加的话有什么影响? ...
- 更改EBS R12中forms的模式Servlet/Socket
EBS R12中forms的模式有:Servlet mode 和 Forms Socket mode 当我们完成Oracle EBS R12套件的快速安装后,forms的默认配置是Servlet mo ...
- Android 几种网络请求的区别与联系
HttpUrlConnection 最开始学android的时候用的网络请求是HttpUrlConnection,当时很多东西还不知道,但是在android 2.2及以下版本中HttpUrlConne ...
- vi/vim下看十六进制文件
:%!xxd --将当前文本转换为16进制格式. 查看内容是对应的.你可以后面看到对应的字符内容 :%!od --将当前文本转换为16进制格式. :%!xxd -c 12--将当前文本转换为16进制格 ...
- Linux进程管理(第二版) --计划任务
计划任务 一.一次性计划任务 月11日) at 5:30pm at 17:30 [today] #today可省略 at now + 3 hours at now + 180 minutes at 1 ...
- iOS多线程篇:NSThread简单介绍和使用
一.什么是NSThread NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程, 需要手动管理线程的生命周期,处理线程同 ...
- java--加强之 Java5的线程并发库
转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9945499 01. 传统线程技术回顾 创建线程的两种传统方式: 1.在Thread子类覆盖的r ...