这篇文章主要介绍了Pyqt5设置返回键和跳转页面键的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
例子一
import sys from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication class First(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.btn = QPushButton("进入页面2", self) self.btn.move(30, 50) self.setGeometry(300, 200, 500, 500) self.setwindowTitle('页面1') class Second(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.btn = QPushButton("进入页面3", self) self.btn.move(30, 50) self.setGeometry(300, 200, 290, 150) self.setwindowTitle('页面2') class Third(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.btn = QPushButton("返回页面1", self) self.btn.move(30, 50) self.btn2 = QPushButton("功能函数", self) self.btn2.move(30, 90) self.setGeometry(500, 200, 290, 350) self.setwindowTitle('页面3') self.btn2.clicked.connect(self.action) def action(self): print("这是功能函数") if __name__ == '__main__': app = QApplication(sys.argv) a = First() b = Second() c = Third() a.show() a.btn.clicked.connect(b.show) a.btn.clicked.connect(a.close) b.btn.clicked.connect(c.show) b.btn.clicked.connect(b.close) c.btn.clicked.connect(a.show) c.btn.clicked.connect(c.close) sys.exit(app.exec_())
例子二
from PyQt5 import QtCore, QtGui, QtWidgets import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QPushButton, QLineEdit, QMenuBar, QStatusBar from PyQt5.QtCore import * class Firstwindow(QWidget): def __init__(self, parent=None): # super这个用法是调用父类的构造函数 # parent=None表示默认没有父Widget,如果指定父亲Widget,则调用之 super(Firstwindow, self).__init__(parent) self.setGeometry(500, 500, 500, 500) self.setwindowTitle('显示') self.btn = QPushButton(self) self.btn.setText('进入页面2') self.btn.move(150, 50) #下面的一部分是Qtdesigner的代码 class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setobjectName("MainWindow") MainWindow.resize(624, 479) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setobjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(190, 130, 51, 31)) self.label.setobjectName("label") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(330, 260, 75, 23)) self.pushButton.setobjectName("pushButton") self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(280, 130, 151, 21)) self.lineEdit.setobjectName("lineEdit") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(190, 180, 51, 31)) self.label_2.setobjectName("label_2") self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit_2.setGeometry(QtCore.QRect(280, 190, 151, 21)) self.lineEdit_2.setobjectName("lineEdit_2") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(200, 260, 75, 23)) self.pushButton_2.setobjectName("pushButton_2") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 624, 23)) self.menubar.setobjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setobjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setwindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", " 长")) self.pushButton.setText(_translate("MainWindow", "返回")) self.label_2.setText(_translate("MainWindow", " 宽")) self.pushButton_2.setText(_translate("MainWindow", "确定")) if __name__ == "__main__": app = QApplication(sys.argv) MainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) ex = Firstwindow() ex.btn.clicked.connect(MainWindow.show) ex.btn.clicked.connect(ex.close) ui.pushButton.clicked.connect(ex.show) ui.pushButton.clicked.connect(MainWindow.close) ex.show() sys.exit(app.exec_())
到此这篇关于Pyqt5设置返回键和跳转页面键的示例代码的文章就介绍到这了,更多相关Pyqt5 返回键和跳转页面键内容请搜索编程之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程之家!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。