繁体   English   中英

使用查找功能突出显示单词并使用彩色光标移动光标并显示找到的单词数

[英]Highlighting the words with find function and moving the cursor with coloured cursor movement and displaying the number of found words

我正在尝试实现查找功能以显示找到的单词并将光标移动到带有彩色光标的找到的单词上并突出显示找到的单词并计算找到的单词数。

我尝试了荧光笔和光标移动但我的荧光笔在光标移动后工作。 mainWindow.py

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import os
import sys
#import Find_Problem
import Dock_Find
class MainWindow(Find_Problem.Ui_MainWindow, QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.actionNew.triggered.connect(self.newFile)
        self.actionOpen.triggered.connect(self.openFile)
        self.actionSave.triggered.connect(self.saveFile)
        self.actionExit.triggered.connect(self.exitFile)
        self.actionFind.triggered.connect(self.new_find)
        self.actionWord_Count.triggered.connect(self.countWord)
        self.show()
        self.showMaximized()

    def newFile(self):
        self.textEdit.clear()

    def openFile(self):
        filename = QFileDialog.getOpenFileName(self, 'Open File', ".","(*.txt)")
        if filename[0]:
            f = open(filename[0], 'rt')
            with f:
                data = f.read()
                self.textEdit.setText(data)

    def saveFile(self):
        filename = QFileDialog.getSaveFileName(self, 'Save File', ".", "(*.txt)")
        if filename[0]:
            f = open(filename[0], 'wt')
            with f:
                text = self.textEdit.toPlainText()
                f.write(text)
                QMessageBox.about(self, "Save File", "File Saved Successfully")

    def exitFile(self):
        choice = QMessageBox.question(self, 'Close', "Do you want to close?", QMessageBox.Yes | QMessageBox.No)
        if choice == QMessageBox.Yes:
            self.saveFile()
            self.close()
        else:
            pass

    def new_find(self):
        txt = Dock_Find()
        txt.show()

        def handleFind():
            text = txt.findLine.text()

            if self.find(text):
                linenumber = self.textCursor().blockNumber() + 1
                fmt = QTextCharFormat()
                fmt.setForeground(Qt.blue)
                return
            else:

                #self.statusBar().showMessage("<b>'" + txt.findLine.text() + "'</b> not found")
                self.moveCursor(QTextCursor.Start)
                if self.find(text):
                    fmt = QTextCharFormat()
                    fmt.setBackground(Qt.yellow)
                    self.mergeFormatOnWordsOrSelection(fmt)
                    linenumber = self.textCursor().blockNumber() + 1
                    return

            QMessageBox.about(self, "No Match", "No Words Found")
            txt.hide()
        def handleFindPrevious():
            text = txt.findLine.text()
            long = len(text)
            cursor = QTextCursor()
            pos = cursor.position()

            if self.find(text):
                linenumber = self.textCursor().blockNumber() +1
                fmt = QTextCharFormat()
                fmt.setForeground(Qt.red)
                self.mergeFormatOnWordsOrSelection(fmt)
                return
            else:
                self.moveCursor(QTextCursor.PreviousWord)
                if self.find(text):
                    fmt = QTextCharFormat()
                    fmt.setBackground(Qt.yellow)
                    self.mergeFormatOnWordsOrSelection(fmt)
                    linenumber = self.textCursor().blockNumber() +1
                    return
        txt.findButton.clicked.connect(handleFind)
        txt.previousButton.clicked.connect(handleFindPrevious)


    def mergeFormatOnWordsSelection(self, format):
        cursor = self.textEdit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        cursor.mergeCharFormat(format)
        self.textEdit.mergeCurrentCharFormat(format)

    def countWord(self):
        text = self.textEdit.textCursor().selectedText()
        words = str(len(text.split()))
        symbols = str(len(text))
        self.currenWords.setText(words)
        self.currentSymbols.setText(symbols)
        text = self.textEdit.toPlainText()
        words = str(len(text.split()))
        symbols = str(len(text))
        self.totalWords.setText(words)
        self.totalSymbols.setText(symbols)


class Dock_Find(Dock_Find.Ui_Dock_Find, QtWidgets.QDockWidget):
    def __init__(self, parent=None):
        super(Dock_Find, self).__init__(parent)
        self.setupUi(self)
        self.findLine.setPlaceholderText("Type Here")


if __name__== '__main__':
    app = QtWidgets.QApplication(sys.argv)
    qt_app = MainWindow()
    qt_app.show()
    sys.exit(app.exec_())

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuSearch = QtWidgets.QMenu(self.menubar)
        self.menuSearch.setObjectName("menuSearch")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")
        self.actionSave = QtWidgets.QAction(MainWindow)
        self.actionSave.setObjectName("actionSave")
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")
        self.actionFind = QtWidgets.QAction(MainWindow)
        self.actionFind.setObjectName("actionFind")
        self.actionWord_Count = QtWidgets.QAction(MainWindow)
        self.actionWord_Count.setObjectName("actionWord_Count")
        self.actionNew = QtWidgets.QAction(MainWindow)
        self.actionNew.setObjectName("actionNew")
        self.menuFile.addAction(self.actionNew)
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave)
        self.menuFile.addAction(self.actionExit)
        self.menuSearch.addAction(self.actionFind)
        self.menuSearch.addAction(self.actionWord_Count)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuSearch.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuSearch.setTitle(_translate("MainWindow", "Search"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))
        self.actionOpen.setShortcut(_translate("MainWindow", "Ctrl+O"))
        self.actionSave.setText(_translate("MainWindow", "Save"))
        self.actionSave.setShortcut(_translate("MainWindow", "Ctrl+S"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))
        self.actionExit.setShortcut(_translate("MainWindow", "Ctrl+Q"))
        self.actionFind.setText(_translate("MainWindow", "Find"))
        self.actionFind.setShortcut(_translate("MainWindow", "Ctrl+F"))
        self.actionWord_Count.setText(_translate("MainWindow", "Word Count"))
        self.actionNew.setText(_translate("MainWindow", "New"))
        self.actionNew.setShortcut(_translate("MainWindow", "Ctrl+N"))
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dock_Find(object):
    def setupUi(self, Dock_Find):
        Dock_Find.setObjectName("Dock_Find")
        Dock_Find.resize(401, 62)
        Dock_Find.setMinimumSize(QtCore.QSize(320, 60))
        font = QtGui.QFont()
        font.setPointSize(10)
        Dock_Find.setFont(font)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/image/graphy_100px.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Dock_Find.setWindowIcon(icon)
        self.dockWidgetContents = QtWidgets.QWidget()
        self.dockWidgetContents.setObjectName("dockWidgetContents")
        self.serachLabel = QtWidgets.QLabel(self.dockWidgetContents)
        self.serachLabel.setGeometry(QtCore.QRect(10, 10, 71, 16))
        self.serachLabel.setObjectName("serachLabel")
        self.findLine = QtWidgets.QLineEdit(self.dockWidgetContents)
        self.findLine.setGeometry(QtCore.QRect(80, 10, 151, 20))
        self.findLine.setObjectName("findLine")
        self.findButton = QtWidgets.QPushButton(self.dockWidgetContents)
        self.findButton.setGeometry(QtCore.QRect(320, 10, 75, 23))
        self.findButton.setObjectName("findButton")
        self.previousButton = QtWidgets.QPushButton(self.dockWidgetContents)
        self.previousButton.setGeometry(QtCore.QRect(240, 10, 75, 23))
        self.previousButton.setObjectName("previousButton")
        Dock_Find.setWidget(self.dockWidgetContents)

        self.retranslateUi(Dock_Find)
        QtCore.QMetaObject.connectSlotsByName(Dock_Find)

    def retranslateUi(self, Dock_Find):
        _translate = QtCore.QCoreApplication.translate
        Dock_Find.setWindowTitle(_translate("Dock_Find", "Find"))
        self.serachLabel.setText(_translate("Dock_Find", "Search For:"))
        self.findButton.setText(_translate("Dock_Find", "Find Next"))
        self.previousButton.setText(_translate("Dock_Find", "Previous"))

import resource_rc

我的光标在默认情况下突出显示,我无法正确看到它,我想要突出显示它,还要查找创建一个函数来计算找到的单词数。 我正在学习开发文本编辑器和新程序员以及使用PyQt5进行GUI。

试试吧:

#import os
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *

#import Find_Problem
#import Dock_Find


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuSearch = QtWidgets.QMenu(self.menubar)
        self.menuSearch.setObjectName("menuSearch")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")
        self.actionSave = QtWidgets.QAction(MainWindow)
        self.actionSave.setObjectName("actionSave")
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")
        self.actionFind = QtWidgets.QAction(MainWindow)
        self.actionFind.setObjectName("actionFind")
        self.actionWord_Count = QtWidgets.QAction(MainWindow)
        self.actionWord_Count.setObjectName("actionWord_Count")
        self.actionNew = QtWidgets.QAction(MainWindow)
        self.actionNew.setObjectName("actionNew")
        self.menuFile.addAction(self.actionNew)
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave)
        self.menuFile.addAction(self.actionExit)
        self.menuSearch.addAction(self.actionFind)
        self.menuSearch.addAction(self.actionWord_Count)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuSearch.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuSearch.setTitle(_translate("MainWindow", "Search"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))
        self.actionOpen.setShortcut(_translate("MainWindow", "Ctrl+O"))
        self.actionSave.setText(_translate("MainWindow", "Save"))
        self.actionSave.setShortcut(_translate("MainWindow", "Ctrl+S"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))
        self.actionExit.setShortcut(_translate("MainWindow", "Ctrl+Q"))
        self.actionFind.setText(_translate("MainWindow", "Find"))
        self.actionFind.setShortcut(_translate("MainWindow", "Ctrl+F"))
        self.actionWord_Count.setText(_translate("MainWindow", "Word Count"))
        self.actionNew.setText(_translate("MainWindow", "New"))
        self.actionNew.setShortcut(_translate("MainWindow", "Ctrl+N"))


class Ui_Dock_Find(object):
    def setupUi(self, Dock_Find):
        Dock_Find.setObjectName("Dock_Find")
        Dock_Find.resize(320, 65)
        Dock_Find.setMinimumSize(QtCore.QSize(320, 65))
        font = QtGui.QFont()
        font.setPointSize(10)
        Dock_Find.setFont(font)
        icon = QtGui.QIcon()

#        icon.addPixmap(QtGui.QPixmap(":/image/graphy_100px.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        icon.addPixmap(QtGui.QPixmap("Ok.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)

        Dock_Find.setWindowIcon(icon)
        self.dockWidgetContents = QtWidgets.QWidget()
        self.dockWidgetContents.setObjectName("dockWidgetContents")
        self.serachLabel = QtWidgets.QLabel(self.dockWidgetContents)
        self.serachLabel.setGeometry(QtCore.QRect(10, 10, 71, 16))
        self.serachLabel.setObjectName("serachLabel")
        self.findLine = QtWidgets.QLineEdit(self.dockWidgetContents)
        self.findLine.setGeometry(QtCore.QRect(80, 10, 151, 20))
        self.findLine.setObjectName("findLine")
        self.findButton = QtWidgets.QPushButton(self.dockWidgetContents)
        self.findButton.setGeometry(QtCore.QRect(240, 10, 75, 23))
        self.findButton.setObjectName("findButton")
        Dock_Find.setWidget(self.dockWidgetContents)

        self.retranslateUi(Dock_Find)
        QtCore.QMetaObject.connectSlotsByName(Dock_Find)

    def retranslateUi(self, Dock_Find):
        _translate = QtCore.QCoreApplication.translate
        Dock_Find.setWindowTitle(_translate("Dock_Find", "Find"))
        self.serachLabel.setText(_translate("Dock_Find", "Search For:"))
        self.findButton.setText(_translate("Dock_Find", "Find"))

#import resource_rc        


#class MainWindow(Find_Problem.Ui_MainWindow, QtWidgets.QMainWindow):
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        self.actionNew.triggered.connect(self.newFile)
        self.actionOpen.triggered.connect(self.openFile)
        self.actionSave.triggered.connect(self.saveFile)
        self.actionExit.triggered.connect(self.exitFile)
        self.actionFind.triggered.connect(self.findWord)
        self.actionWord_Count.triggered.connect(self.countWord)
        self.show()
        self.showMaximized()

        self.textEdit.setFont(QFont('Decorative', 12))                      # +
        self.countWords = 0                                                 # +++

    def newFile(self):
        self.textEdit.clear()

    def openFile(self):
        filename = QFileDialog.getOpenFileName(self, 'Open File', ".","(*.txt *.py)")   # + *.py
        if filename[0]:
            fmt = QTextCharFormat()
            fmt.setForeground(QColor(0, 0, 0))
            fmt.setFontPointSize(12)
            self.textEdit.mergeCurrentCharFormat(fmt)

            f = open(filename[0], 'rt')
            with f:
                data = f.read()
                self.textEdit.setText(data)

    def saveFile(self):
        filename = QFileDialog.getSaveFileName(self, 'Save File', ".", "(*.txt)")
        if filename[0]:
            f = open(filename[0], 'wt')
            with f:
                text = self.textEdit.toPlainText()
                f.write(text)
                QMessageBox.about(self, "Save File", "File Saved Successfully")

    def exitFile(self):
        choice = QMessageBox.question(self, 'Close', "Do you want to close?", QMessageBox.Yes | QMessageBox.No)
        if choice == QMessageBox.Yes:
            self.saveFile()
            self.close()
        else:
            pass

    def findWord(self):

        self.dock = Dock_Find()
        self.addDockWidget(Qt.TopDockWidgetArea, self.dock)
        self.dock.show()
        self.dock.findButton.clicked.connect(self.handleFind)

### +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

    def mergeFormatOnWordOrSelection(self, format):
        cursor = self.textEdit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        cursor.mergeCharFormat(format)
        self.textEdit.mergeCurrentCharFormat(format)

    def handleFind(self):
        text = self.dock.findLine.text()
        if not text:
            return
        col = QColorDialog.getColor(self.textEdit.textColor(), self)
        if not col.isValid():
            return
        fmt = QTextCharFormat()
        fmt.setForeground(col)
        print("\nfmt.setForeground(col)", col)
        fmt.setFontPointSize(14)     

        self.textEdit.moveCursor(QTextCursor.Start)

        self.countWords = 0
        while self.textEdit.find(text, QTextDocument.FindWholeWords):      # Find whole words
            self.mergeFormatOnWordOrSelection(fmt)
            self.countWords += 1

        QMessageBox.information(self, 
            "Information", 
#            f"word->`{text}` found in the text `{self.countWords}` times."
             "word->`{text}` found in the text `{countWords}` times.".format(text=text, countWords=self.countWords)
        )

### +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    

    """ ---
        self.dock = Dock_Find()
        self.addDockWidget(Qt.TopDockWidgetArea, self.dock)
        self.dock.show()

        def handleFind():
            text = self.dock.findLine.text()
            if self.textEdit.find(text):
                return
            else:
                fmt = QTextCharFormat()
                fmt.setBackground(Qt.yellow)
                self.textEdit.moveCursor(QTextCursor.Start)
                while self.textEdit.find(text, QTextDocument.FindWholeWords):
                    self.mergeFormatOnWordsSelection(fmt)
                if self.textEdit.find(text):
                    while self.textEdit.moveCursor(QTextCursor.EndOfWord):
                        QMessageBox.about(self, "End of Line", "No Further More Words")
                    return
            QMessageBox.about(self, "No Match", "No Words Found")
        self.dock.findButton.clicked.connect(handleFind)

    def mergeFormatOnWordsSelection(self, format):
        cursor = self.textEdit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        cursor.mergeCharFormat(format)
        self.textEdit.mergeCurrentCharFormat(format)
    """ 

    def countWord(self):
        textWord    = self.textEdit.textCursor().selectedText()
        words   = str(len(textWord.split()))
        symbols = str(len(textWord))

#?        self.currenWords.setText(words)
#?        self.currentSymbols.setText(symbols)

        text = self.textEdit.toPlainText()
        words = str(len(text.split()))
        symbols = str(len(text))

#?        self.totalWords.setText(words)
#?        self.totalSymbols.setText(symbols)

#        print(f"word->`{self.dock.findLine.text()}` found in the text `{self.countWords}` times.") # +
        print("word->`{text}` found in the text `{countWords}` times."
              "".format(text=self.dock.findLine.text(), countWords=self.countWords)) # +

        QMessageBox.information(self, 
            "Information", 
#            f"word->`{textWord}` found in the text `{self.countWords}` times."
            "word->`{textWord}` found in the text `{countWords}` times."
            "".format(textWord=textWord, countWords=self.countWords)
        )


#class Dock_Find(Dock_Find.Ui_Dock_Find, QtWidgets.QDockWidget):
class Dock_Find(QtWidgets.QDockWidget, Ui_Dock_Find):
    def __init__(self, parent=None):
        super(Dock_Find, self).__init__(parent)
        self.setupUi(self)
        self.findLine.setPlaceholderText("Type Here")


if __name__== '__main__':
    app = QtWidgets.QApplication(sys.argv)
    qt_app = MainWindow()
    qt_app.show()
    sys.exit(app.exec_())

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM