功能:
1.支持 正则表达式 查找
2.支持 向上查找
3.支持 区分大小写 查找
4.支持 整个单词 查找
5.支持 循环查找
效果图:
代码:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import *
import sys
class FindAndReplace(QWidget):
def __init__(self, *arg, **kwarg):
super(self.__class__, self).__init__(*arg, **kwarg)
rows = QVBoxLayout()
self.editor = QsciScintilla()
self.editor.setText(f'{"x"*40}\n{"y"*40}\n{"z"*40}\n')
rows.addWidget(self.editor)
self.text_to_find = ''
self.state_ = tuple()
self.find = QLineEdit()
self.find_previous = QPushButton('&Previous')
self.find_next = QPushButton('&Next')
self.find_lbl = QLabel('&Find')
self.find_lbl.setBuddy(self.find)
row = QHBoxLayout()
for w in (self.find_lbl, self.find, self.find_previous, self.find_next):
row.addWidget(w)
rows.addLayout(row)
self.re = QCheckBox('&Regular expressions')
self.cs = QCheckBox('&Case sensitive')
self.wo = QCheckBox('Whole &words')
self.wrap = QCheckBox('Wrap aroun&d')
self.show_ = QCheckBox('&Unfold folded text')
self.posix = QCheckBox('POSI&X-compatible RE')
row = QHBoxLayout()
for w in (self.re, self.cs, self.wo, self.wrap, self.show_, self.posix):
row.addWidget(w)
rows.addLayout(row)
self.setLayout(rows)
self.find_previous.clicked.connect(lambda: self.findText(forward = False))
self.find_next.clicked.connect(lambda: self.findText(forward = True))
def findText(self, forward):
text_to_find = self.find.text()
if forward:
line, index = self.editor.getSelection()[2:]
else:
line, index = self.editor.getSelection()[:2]
state_ = (
self.re.isChecked(), self.cs.isChecked(),
self.wo.isChecked(), self.wrap.isChecked(),
forward, line, index,
self.show_.isChecked(), self.posix.isChecked(),
)
self.text_to_find = text_to_find
self.state_ = state_
self.editor.findFirst(text_to_find, *state_)
if __name__ == '__main__':
app = QApplication(sys.argv)
FindAndReplace().show()
sys.exit(app.exec_())