您好,登錄后才能下訂單哦!
這篇文章主要介紹了Pyqt5如何設(shè)置返回鍵和跳轉(zhuǎn)頁面鍵,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
例子一
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("進(jìn)入頁面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("進(jìn)入頁面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("功能函數(shù)", 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("這是功能函數(shù)") 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這個(gè)用法是調(diào)用父類的構(gòu)造函數(shù) # parent=None表示默認(rèn)沒有父Widget,如果指定父親Widget,則調(diào)用之 super(FirstWindow, self).__init__(parent) self.setGeometry(500, 500, 500, 500) self.setWindowTitle('顯示') self.btn = QPushButton(self) self.btn.setText('進(jìn)入頁面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_())
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Pyqt5如何設(shè)置返回鍵和跳轉(zhuǎn)頁面鍵”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。