用python3+PyQt5改写Python Qt GUI快速编程的第七章实例Find Dialog
来源:互联网 发布:ch341a编程器1.3破解 编辑:程序博客网 时间:2024/05/22 17:20
本文用用python3+PyQt5改写Python Qt GUI快速编程的第七章实例Find Dialog,通过Qt Designer设计界面。代码与设计分离,非常快捷。
/home/yrd/eric_workspace/FR_Dialog/Ui_FR_Dialog.py
# -*- coding: utf-8 -*-from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(417, 192) Dialog.setSizeGripEnabled(True) self.line = QtWidgets.QFrame(Dialog) self.line.setGeometry(QtCore.QRect(300, 10, 20, 171)) self.line.setFrameShape(QtWidgets.QFrame.VLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.widget = QtWidgets.QWidget(Dialog) self.widget.setGeometry(QtCore.QRect(10, 10, 281, 171)) self.widget.setObjectName("widget") self.verticalLayout = QtWidgets.QVBoxLayout(self.widget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.formLayout = QtWidgets.QFormLayout() self.formLayout.setObjectName("formLayout") self.findLineLabel = QtWidgets.QLabel(self.widget) self.findLineLabel.setObjectName("findLineLabel") self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.findLineLabel) self.findLineEdit = QtWidgets.QLineEdit(self.widget) self.findLineEdit.setObjectName("findLineEdit") self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.findLineEdit) self.replaceLineLabel = QtWidgets.QLabel(self.widget) self.replaceLineLabel.setObjectName("replaceLineLabel") self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.replaceLineLabel) self.replaceLineEdit = QtWidgets.QLineEdit(self.widget) self.replaceLineEdit.setObjectName("replaceLineEdit") self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.replaceLineEdit) self.verticalLayout.addLayout(self.formLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.caseCheckBox = QtWidgets.QCheckBox(self.widget) self.caseCheckBox.setObjectName("caseCheckBox") self.horizontalLayout_2.addWidget(self.caseCheckBox) self.wholeCheckBox = QtWidgets.QCheckBox(self.widget) self.wholeCheckBox.setEnabled(True) self.wholeCheckBox.setChecked(True) self.wholeCheckBox.setObjectName("wholeCheckBox") self.horizontalLayout_2.addWidget(self.wholeCheckBox) self.verticalLayout.addLayout(self.horizontalLayout_2) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.SyntaxLabel = QtWidgets.QLabel(self.widget) self.SyntaxLabel.setObjectName("SyntaxLabel") self.horizontalLayout_3.addWidget(self.SyntaxLabel) self.syntaxComboBox = QtWidgets.QComboBox(self.widget) self.syntaxComboBox.setObjectName("syntaxComboBox") self.syntaxComboBox.addItem("") self.syntaxComboBox.addItem("") self.horizontalLayout_3.addWidget(self.syntaxComboBox) self.verticalLayout.addLayout(self.horizontalLayout_3) spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem) self.widget1 = QtWidgets.QWidget(Dialog) self.widget1.setGeometry(QtCore.QRect(330, 10, 71, 171)) self.widget1.setObjectName("widget1") self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget1) self.verticalLayout_2.setContentsMargins(0, 0, 0, 0) self.verticalLayout_2.setObjectName("verticalLayout_2") self.findButton = QtWidgets.QPushButton(self.widget1) self.findButton.setObjectName("findButton") self.verticalLayout_2.addWidget(self.findButton) self.replaceButton = QtWidgets.QPushButton(self.widget1) self.replaceButton.setObjectName("replaceButton") self.verticalLayout_2.addWidget(self.replaceButton) self.replaceAllButton = QtWidgets.QPushButton(self.widget1) self.replaceAllButton.setObjectName("replaceAllButton") self.verticalLayout_2.addWidget(self.replaceAllButton) spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout_2.addItem(spacerItem1) self.closeButton = QtWidgets.QPushButton(self.widget1) self.closeButton.setObjectName("closeButton") self.verticalLayout_2.addWidget(self.closeButton) self.findLineLabel.setBuddy(self.findLineEdit) self.replaceLineLabel.setBuddy(self.replaceLineEdit) self.SyntaxLabel.setBuddy(self.syntaxComboBox) self.retranslateUi(Dialog) self.closeButton.clicked.connect(Dialog.reject) QtCore.QMetaObject.connectSlotsByName(Dialog) Dialog.setTabOrder(self.findLineEdit, self.replaceLineEdit) Dialog.setTabOrder(self.replaceLineEdit, self.caseCheckBox) Dialog.setTabOrder(self.caseCheckBox, self.wholeCheckBox) Dialog.setTabOrder(self.wholeCheckBox, self.syntaxComboBox) Dialog.setTabOrder(self.syntaxComboBox, self.findButton) Dialog.setTabOrder(self.findButton, self.replaceButton) Dialog.setTabOrder(self.replaceButton, self.replaceAllButton) Dialog.setTabOrder(self.replaceAllButton, self.closeButton) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.findLineLabel.setText(_translate("Dialog", "Find &what:")) self.replaceLineLabel.setText(_translate("Dialog", "Replace w&ith")) self.caseCheckBox.setText(_translate("Dialog", "&Case sensitive")) self.wholeCheckBox.setText(_translate("Dialog", "Wh&ole words")) self.SyntaxLabel.setText(_translate("Dialog", "&Syntax:")) self.syntaxComboBox.setItemText(0, _translate("Dialog", "Literal text")) self.syntaxComboBox.setItemText(1, _translate("Dialog", "Regular expression")) self.findButton.setText(_translate("Dialog", "&Find")) self.replaceButton.setText(_translate("Dialog", "&Replace")) self.replaceAllButton.setText(_translate("Dialog", "Replace &All")) self.closeButton.setText(_translate("Dialog", "Close"))
/home/yrd/eric_workspace/FR_Dialog/FR_Dialog.py
# -*- coding: utf-8 -*-"""Module implementing FR_Dialog."""from PyQt5.QtCore import pyqtSlot,pyqtSignal,Qtfrom PyQt5.QtWidgets import QDialog,QApplicationfrom Ui_FR_Dialog import Ui_Dialogimport reclass FR_Dialog(QDialog, Ui_Dialog): """ Class documentation goes here. """ nofound = pyqtSignal() found = pyqtSignal(int) def __init__(self,text,parent=None): """ Constructor @param parent reference to the parent widget @type QWidget """ super(FR_Dialog, self).__init__(parent) self.__text = text self.__index = 0 self.setupUi(self) MAC = "qt_mac_set_native_menubar" in dir() if not MAC: self.findButton.setFocusPolicy(Qt.NoFocus) self.replaceButton.setFocusPolicy(Qt.NoFocus) self.replaceAllButton.setFocusPolicy(Qt.NoFocus) self.closeButton.setFocusPolicy(Qt.NoFocus) self.updateUi() @pyqtSlot(str) def on_findLineEdit_textEdited(self, text): self.__index = 0 self.updateUi() def makeRegex(self): findText = self.findLineEdit.text() if self.syntaxComboBox.currentText() == "Literal": findText = re.escape(findText) flags = re.MULTILINE|re.DOTALL|re.UNICODE if not self.caseCheckBox.isChecked(): flags |= re.IGNORECASE if self.wholeCheckBox.isChecked(): findText = r"\b%s\b" % findText return re.compile(findText, flags) @pyqtSlot() def on_findButton_clicked(self): regex = self.makeRegex() match = regex.search(self.__text, self.__index) if match is not None: self.__index = match.end() #self.emit(SIGNAL("found"), match.start()) self.found.emit(match.start()) else: #self.emit(SIGNAL("notfound")) self.nofound.emit() @pyqtSlot() def on_replaceButton_clicked(self): regex = self.makeRegex() self.__text = regex.sub(self.replaceLineEdit.text(), self.__text, 1) @pyqtSlot() def on_replaceAllButton_clicked(self): regex = self.makeRegex() self.__text = regex.sub(self.replaceLineEdit.text(), self.__text) def updateUi(self): enable = bool(self.findLineEdit.text()) self.findButton.setEnabled(enable) self.replaceButton.setEnabled(enable) self.replaceAllButton.setEnabled(enable) def text(self): return self.__text if __name__ == "__main__": import sys text = """US experience shows that, unlike traditional patents,software patents do not encourage innovation and R&D, quite thecontrary. In particular they hurt small and medium-sized enterprisesand generally newcomers in the market. They will just weaken the marketand increase spending on patents and litigation, at the expense oftechnological innovation and research. Especially dangerous areattempts to abuse the patent system by preventing interoperability as ameans of avoiding competition with technological ability.--- Extract quoted from Linus Torvalds and Alan Cox's letterto the President of the European Parliamenthttp://www.effi.org/patentit/patents_torvalds_cox.html""" app = QApplication(sys.argv) form = FR_Dialog(text) #form.connect(form, SIGNAL("found"), found) #form.connect(form, SIGNAL("notfound"), nomore) def found(where): print("Found at %d" % where) def nomore(): print("No more found") form.found.connect(found) form.nofound.connect(nomore) form.show() sys.exit(app.exec_()) #print form.text()
运行结果:
0 0
- 用python3+PyQt5改写Python Qt GUI快速编程的第七章实例Find Dialog
- 用python3+PyQt5改写Python Qt GUI快速编程的第6章实例主窗口
- 用python3+PyQt5改写Python Qt GUI快速编程的第6章实例主窗口 信号及槽如何使用
- Python Qt GUI快速编程-第四章的digit的python3+pytq5+Qt Designer实现
- Python Qt GUI快速编程-第四章的Currency例子的python3+pytq5+Qt Designer实现
- Python Qt GUI快速编程-第五章的例子pen的python3+pytq5+Qt Designer实现
- Python Qt GUI快速编程-第五章的exercise的python3+pytq5+Qt Dersinger实现
- Python Qt GUI快速编程-第五章的例子Numbers的python3+pytq5+Qt Designer实现
- Python Qt GUI快速编程-第四章的计算器python3+pytq5+Qt Designer实现
- Python Qt GUI快速编程-第四章的Exercise例子Interest的python3+pytq5+Qt Designer实现
- Python Qt GUI快速编程第六章代码分析
- python3.6+pyqt5+pycharm5界面编程实例
- 用 eric6 与 PyQt5 实现python的极速GUI编程(系列01)--Hello world!
- Ubantu环境下利用Python3+PyQt5+Eric6进行GUI编程
- Ubuntu环境下利用Python3+PyQt5+Eric6进行GUI编程
- PyQt5 环境配置和Python GUI编程介绍
- python3.6+pyqt5 IDE: Pycahrm+Qtdesign 快速开发GUI程序学习资源
- 快速熟悉 PyQt5 与 Eric6 的极速 GUI 开发
- Linux文件管理常用命令
- ibatis多对多示例
- 中国剩余定理与扩展 Lucas定理与扩展 学习笔记
- 2016总结
- java零基础入门知识2.1——封装(面向对象的特征之一)
- 用python3+PyQt5改写Python Qt GUI快速编程的第七章实例Find Dialog
- android 开机自启动应用 (详细)
- 详解Linux进程及作业管理
- 学表单的第二天
- 微信小程序api调起微信提供的功能-网络、媒体、文件、数据存储、位置、设备、界面、开发接口
- Markdown常用语法
- CentOS - 添加删除用户
- 软件设计是怎样炼成的(2)——优秀设计从分析需求开始
- 选择器的补充