python3+PyQt5+Qt Designer实现扩展对话框
程序员文章站
2023-10-18 20:35:57
本文是对《python qt gui快速编程》的第9章的扩展对话框例子find and replace用python3+pyqt5+qt designer进行改写。...
本文是对《python qt gui快速编程》的第9章的扩展对话框例子find and replace用python3+pyqt5+qt designer进行改写。
第一部分无借用qt designer,完全用代码实现。
第二部分则借用qt designer,快速实现。
第一部分:
import sys from pyqt5.qtcore import qt,pyqtsignal from pyqt5.qtwidgets import (qapplication, qcheckbox, qdialog, qframe, qgridlayout, qhboxlayout, qlabel, qlayout, qlineedit, qpushbutton, qvboxlayout) class findandreplacedlg(qdialog): find = pyqtsignal(str,bool,bool,bool,bool,bool) replace = pyqtsignal(str,str,bool,bool,bool,bool,bool) def __init__(self, parent=none): super(findandreplacedlg, self).__init__(parent) findlabel = qlabel("find &what:") self.findlineedit = qlineedit() findlabel.setbuddy(self.findlineedit) replacelabel = qlabel("replace w&ith:") self.replacelineedit = qlineedit() replacelabel.setbuddy(self.replacelineedit) self.casecheckbox = qcheckbox("&case sensitive") self.wholecheckbox = qcheckbox("wh&ole words") self.wholecheckbox.setchecked(true) self.moreframe = qframe() self.moreframe.setframestyle(qframe.styledpanel|qframe.sunken) self.backwardscheckbox = qcheckbox("search &backwards") self.regexcheckbox = qcheckbox("regular e&xpression") self.ignorenotescheckbox = qcheckbox("ignore foot¬es " "and endnotes") line = qframe() line.setframestyle(qframe.vline|qframe.sunken) self.findbutton = qpushbutton("&find") self.replacebutton = qpushbutton("&replace") closebutton = qpushbutton("close") self.morebutton = qpushbutton("&more") self.morebutton.setcheckable(true) gridlayout = qgridlayout() gridlayout.addwidget(findlabel, 0, 0) gridlayout.addwidget(self.findlineedit, 0, 1) gridlayout.addwidget(replacelabel, 1, 0) gridlayout.addwidget(self.replacelineedit, 1, 1) framelayout = qvboxlayout() framelayout.addwidget(self.backwardscheckbox) framelayout.addwidget(self.regexcheckbox) framelayout.addwidget(self.ignorenotescheckbox) self.moreframe.setlayout(framelayout) leftlayout = qvboxlayout() leftlayout.addlayout(gridlayout) leftlayout.addwidget(self.casecheckbox) leftlayout.addwidget(self.wholecheckbox) leftlayout.addwidget(self.moreframe) buttonlayout = qvboxlayout() buttonlayout.addwidget(self.findbutton) buttonlayout.addwidget(self.replacebutton) buttonlayout.addwidget(closebutton) buttonlayout.addwidget(self.morebutton) buttonlayout.addstretch() mainlayout = qhboxlayout() mainlayout.addlayout(leftlayout) mainlayout.addwidget(line) mainlayout.addlayout(buttonlayout) self.setlayout(mainlayout) self.moreframe.hide() mainlayout.setsizeconstraint(qlayout.setfixedsize) self.morebutton.toggled[bool].connect(self.setvisible) self.findlineedit.textedited.connect(self.updateui) self.findbutton.clicked.connect(self.findclicked) self.replacebutton.clicked.connect(self.replaceclicked) self.updateui() self.setwindowtitle("find and replace") def setvisible(self,yn): self.moreframe.setvisible(yn) def findclicked(self): self.find.emit(self.findlineedit.text(), self.casecheckbox.ischecked(), self.wholecheckbox.ischecked(), self.backwardscheckbox.ischecked(), self.regexcheckbox.ischecked(), self.ignorenotescheckbox.ischecked()) def replaceclicked(self): self.replace.emit(self.findlineedit.text(), self.replacelineedit.text(), self.casecheckbox.ischecked(), self.wholecheckbox.ischecked(), self.backwardscheckbox.ischecked(), self.regexcheckbox.ischecked(), self.ignorenotescheckbox.ischecked()) def updateui(self): enable = self.findlineedit.text() self.findbutton.setenabled(bool(enable)) self.replacebutton.setenabled(bool(enable)) if __name__ == "__main__": def find(what, *args): print("find {0} {1}".format(what, [x for x in args])) def replace(old, new, *args): print("replace {0} with {1} {2}".format( old, new, [x for x in args])) app = qapplication(sys.argv) form = findandreplacedlg() form.find.connect(find) form.replace.connect(replace) form.show() app.exec_()
第二部分:
/home/yrd/eric_workspace/chap09/findandreplacedlg/ui_findandreplacedlg.py
# -*- coding: utf-8 -*- # form implementation generated from reading ui file '/home/yrd/eric_workspace/chap09/findandreplacedlg/findandreplacedlg.ui' # # created by: pyqt5 ui code generator 5.7 # # warning! all changes made in this file will be lost! from pyqt5 import qtcore, qtgui, qtwidgets class ui_findandreplacedlg(object): def setupui(self, findandreplacedlg): findandreplacedlg.setobjectname("findandreplacedlg") findandreplacedlg.resize(355, 274) self.mainlayout = qtwidgets.qhboxlayout(findandreplacedlg) self.mainlayout.setsizeconstraint(qtwidgets.qlayout.setfixedsize) self.mainlayout.setcontentsmargins(9, 9, 9, 9) self.mainlayout.setspacing(6) self.mainlayout.setobjectname("mainlayout") self.vboxlayout = qtwidgets.qvboxlayout() self.vboxlayout.setcontentsmargins(0, 0, 0, 0) self.vboxlayout.setspacing(6) self.vboxlayout.setobjectname("vboxlayout") self.gridlayout = qtwidgets.qgridlayout() self.gridlayout.setcontentsmargins(0, 0, 0, 0) self.gridlayout.setspacing(6) self.gridlayout.setobjectname("gridlayout") self.replacelineedit = qtwidgets.qlineedit(findandreplacedlg) self.replacelineedit.setobjectname("replacelineedit") self.gridlayout.addwidget(self.replacelineedit, 1, 1, 1, 1) self.findlineedit = qtwidgets.qlineedit(findandreplacedlg) self.findlineedit.setobjectname("findlineedit") self.gridlayout.addwidget(self.findlineedit, 0, 1, 1, 1) self.label_2 = qtwidgets.qlabel(findandreplacedlg) self.label_2.setobjectname("label_2") self.gridlayout.addwidget(self.label_2, 1, 0, 1, 1) self.label = qtwidgets.qlabel(findandreplacedlg) self.label.setobjectname("label") self.gridlayout.addwidget(self.label, 0, 0, 1, 1) self.vboxlayout.addlayout(self.gridlayout) self.vboxlayout1 = qtwidgets.qvboxlayout() self.vboxlayout1.setcontentsmargins(0, 0, 0, 0) self.vboxlayout1.setspacing(6) self.vboxlayout1.setobjectname("vboxlayout1") self.casecheckbox = qtwidgets.qcheckbox(findandreplacedlg) self.casecheckbox.setobjectname("casecheckbox") self.vboxlayout1.addwidget(self.casecheckbox) self.wholecheckbox = qtwidgets.qcheckbox(findandreplacedlg) self.wholecheckbox.setchecked(true) self.wholecheckbox.setobjectname("wholecheckbox") self.vboxlayout1.addwidget(self.wholecheckbox) self.vboxlayout.addlayout(self.vboxlayout1) spaceritem = qtwidgets.qspaceritem(231, 16, qtwidgets.qsizepolicy.minimum, qtwidgets.qsizepolicy.expanding) self.vboxlayout.additem(spaceritem) self.moreframe = qtwidgets.qframe(findandreplacedlg) self.moreframe.setframeshape(qtwidgets.qframe.styledpanel) self.moreframe.setframeshadow(qtwidgets.qframe.raised) self.moreframe.setobjectname("moreframe") self.vboxlayout2 = qtwidgets.qvboxlayout(self.moreframe) self.vboxlayout2.setcontentsmargins(9, 9, 9, 9) self.vboxlayout2.setspacing(6) self.vboxlayout2.setobjectname("vboxlayout2") self.backwardscheckbox = qtwidgets.qcheckbox(self.moreframe) self.backwardscheckbox.setobjectname("backwardscheckbox") self.vboxlayout2.addwidget(self.backwardscheckbox) self.regexcheckbox = qtwidgets.qcheckbox(self.moreframe) self.regexcheckbox.setobjectname("regexcheckbox") self.vboxlayout2.addwidget(self.regexcheckbox) self.ignorenotescheckbox = qtwidgets.qcheckbox(self.moreframe) self.ignorenotescheckbox.setobjectname("ignorenotescheckbox") self.vboxlayout2.addwidget(self.ignorenotescheckbox) self.vboxlayout.addwidget(self.moreframe) self.mainlayout.addlayout(self.vboxlayout) self.line = qtwidgets.qframe(findandreplacedlg) self.line.setframeshape(qtwidgets.qframe.vline) self.line.setframeshadow(qtwidgets.qframe.sunken) self.line.setobjectname("line") self.mainlayout.addwidget(self.line) self.vboxlayout3 = qtwidgets.qvboxlayout() self.vboxlayout3.setcontentsmargins(0, 0, 0, 0) self.vboxlayout3.setspacing(6) self.vboxlayout3.setobjectname("vboxlayout3") self.findbutton = qtwidgets.qpushbutton(findandreplacedlg) self.findbutton.setfocuspolicy(qtcore.qt.nofocus) self.findbutton.setobjectname("findbutton") self.vboxlayout3.addwidget(self.findbutton) self.replacebutton = qtwidgets.qpushbutton(findandreplacedlg) self.replacebutton.setfocuspolicy(qtcore.qt.nofocus) self.replacebutton.setobjectname("replacebutton") self.vboxlayout3.addwidget(self.replacebutton) self.closebutton = qtwidgets.qpushbutton(findandreplacedlg) self.closebutton.setfocuspolicy(qtcore.qt.nofocus) self.closebutton.setobjectname("closebutton") self.vboxlayout3.addwidget(self.closebutton) self.morebutton = qtwidgets.qpushbutton(findandreplacedlg) self.morebutton.setfocuspolicy(qtcore.qt.nofocus) self.morebutton.setcheckable(true) self.morebutton.setobjectname("morebutton") self.vboxlayout3.addwidget(self.morebutton) spaceritem1 = qtwidgets.qspaceritem(21, 16, qtwidgets.qsizepolicy.minimum, qtwidgets.qsizepolicy.expanding) self.vboxlayout3.additem(spaceritem1) self.mainlayout.addlayout(self.vboxlayout3) self.label_2.setbuddy(self.replacelineedit) self.label.setbuddy(self.findlineedit) self.retranslateui(findandreplacedlg) self.closebutton.clicked.connect(findandreplacedlg.reject) self.morebutton.toggled['bool'].connect(self.moreframe.setvisible) qtcore.qmetaobject.connectslotsbyname(findandreplacedlg) findandreplacedlg.settaborder(self.findlineedit, self.replacelineedit) findandreplacedlg.settaborder(self.replacelineedit, self.casecheckbox) findandreplacedlg.settaborder(self.casecheckbox, self.wholecheckbox) findandreplacedlg.settaborder(self.wholecheckbox, self.backwardscheckbox) findandreplacedlg.settaborder(self.backwardscheckbox, self.regexcheckbox) findandreplacedlg.settaborder(self.regexcheckbox, self.ignorenotescheckbox) def retranslateui(self, findandreplacedlg): _translate = qtcore.qcoreapplication.translate findandreplacedlg.setwindowtitle(_translate("findandreplacedlg", "find and replace")) self.label_2.settext(_translate("findandreplacedlg", "replace w&ith:")) self.label.settext(_translate("findandreplacedlg", "find &what:")) self.casecheckbox.settext(_translate("findandreplacedlg", "&case sensitive")) self.wholecheckbox.settext(_translate("findandreplacedlg", "wh&ole words")) self.backwardscheckbox.settext(_translate("findandreplacedlg", "search &backwards")) self.regexcheckbox.settext(_translate("findandreplacedlg", "regular e&xpression")) self.ignorenotescheckbox.settext(_translate("findandreplacedlg", "ignore foot¬es and endnotes")) self.findbutton.settext(_translate("findandreplacedlg", "&find")) self.replacebutton.settext(_translate("findandreplacedlg", "&replace")) self.closebutton.settext(_translate("findandreplacedlg", "close")) self.morebutton.settext(_translate("findandreplacedlg", "&more"))
/home/yrd/eric_workspace/chap09/findandreplacedlg/findandreplacedlg.py
# -*- coding: utf-8 -*- """ module implementing findandreplacedlg. """ from pyqt5.qtcore import pyqtslot,pyqtsignal from pyqt5.qtwidgets import qdialog,qapplication from ui_findandreplacedlg import ui_findandreplacedlg class findandreplacedlg(qdialog, ui_findandreplacedlg): """ class documentation goes here. """ find = pyqtsignal(str,bool,bool,bool,bool,bool) replace = pyqtsignal(str,str,bool,bool,bool,bool,bool) def __init__(self, parent=none): """ constructor @param parent reference to the parent widget @type qwidget """ super(findandreplacedlg, self).__init__(parent) self.setupui(self) self.moreframe.hide() #self.layout().setsizeconstraint(qlayout.setfixedsize) self.updateui() @pyqtslot(str) def on_findlineedit_textedited(self, text): """ slot documentation goes here. @param p0 description @type str """ # todo: not implemented yet self.updateui() @pyqtslot() def on_findbutton_clicked(self): self.find.emit(self.findlineedit.text(), self.casecheckbox.ischecked(), self.wholecheckbox.ischecked(), self.backwardscheckbox.ischecked(), self.regexcheckbox.ischecked(), self.ignorenotescheckbox.ischecked()) @pyqtslot() def on_replacebutton_clicked(self): self.replace.emit(self.findlineedit.text(), self.replacelineedit.text(), self.casecheckbox.ischecked(), self.wholecheckbox.ischecked(), self.backwardscheckbox.ischecked(), self.regexcheckbox.ischecked(), self.ignorenotescheckbox.ischecked()) def updateui(self): enable = self.findlineedit.text() self.findbutton.setenabled(bool(enable)) self.replacebutton.setenabled(bool(enable)) if __name__ == "__main__": import sys def find(what, *args): print("find {0} {1}".format(what, [x for x in args])) def replace(old, new, *args): print("replace {0} with {1} {2}".format( old, new, [x for x in args])) app = qapplication(sys.argv) form = findandreplacedlg() form.find.connect(find) form.replace.connect(replace) form.show() app.exec_()
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。