您的位置:首页 > 编程语言 > Python开发

用python3+PyQt5改写Python Qt GUI快速编程的第七章实例Find Dialog

2017-01-16 09:30 771 查看
本文用用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, QtWidgets

class 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,Qt
from PyQt5.QtWidgets import QDialog,QApplication

from Ui_FR_Dialog import Ui_Dialog
import re

class 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 the
contrary. In particular they hurt small and medium-sized enterprises
and generally newcomers in the market. They will just weaken the market
and increase spending on patents and litigation, at the expense of
technological innovation and research. Especially dangerous are
attempts to abuse the patent system by preventing interoperability as a
means of avoiding competition with technological ability.
--- Extract quoted from Linus Torvalds and Alan Cox's letter
to the President of the European Parliament http://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()


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python qt pyqt