pyqt5使用按钮进行界面的跳转方法
程序员文章站
2022-05-25 21:41:03
简介
进行按钮进行界面的跳转,我这里面我介绍两种,一种是没有使用qtdesigner的代码,另一种是使用qtdesigner的代码
代码1
import sy...
简介
进行按钮进行界面的跳转,我这里面我介绍两种,一种是没有使用qtdesigner的代码,另一种是使用qtdesigner的代码
代码1
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("button", self) self.btn.move(30, 50) self.setgeometry(300, 300, 500, 500) self.setwindowtitle('event sender') self.show() class second(qmainwindow): def __init__(self): super().__init__() self.initui() def initui(self): self.setgeometry(300, 300, 290, 150) self.setwindowtitle('get sender') 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的代码
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('标定') 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.show() sys.exit(app.exec_())
以上这篇pyqt5使用按钮进行界面的跳转方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。