pyqt5使用按钮进行界面的跳转方法
简介
进行按钮进行界面的跳转,我这里面我介绍两种,一种是没有使用Qtdesigner的代码,另一种是使用Qtdesigner的代码
代码1
importsys fromPyQt5.QtWidgetsimportQMainWindow,QPushButton,QApplication classFirst(QMainWindow): def__init__(self): super().__init__() self.initUI() definitUI(self): self.btn=QPushButton("Button",self) self.btn.move(30,50) self.setGeometry(300,300,500,500) self.setWindowTitle('Eventsender') self.show() classSecond(QMainWindow): def__init__(self): super().__init__() self.initUI() definitUI(self): self.setGeometry(300,300,290,150) self.setWindowTitle('Getsender') if__name__=='__main__': app=QApplication(sys.argv) a=First() b=Second() a.show() a.btn.clicked.connect(b.show) sys.exit(app.exec_())
代码2
下面的代码使用了Qtdesigner的代码
fromPyQt5importQtCore,QtGui,QtWidgets importsys fromPyQt5.QtWidgetsimportQApplication,QMainWindow,QWidget,QLabel,\ QPushButton,QLineEdit,QMenuBar,QStatusBar fromPyQt5.QtCoreimport* classFirstWindow(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('标定') self.btn.move(150,50) #下面的一部分是Qtdesigner的代码 classUi_MainWindow(object): defsetupUi(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) defretranslateUi(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.show() sys.exit(app.exec_())
以上这篇pyqt5使用按钮进行界面的跳转方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。