如何在python网络浏览器中突出显示文本,如查找文本

Posted

技术标签:

【中文标题】如何在python网络浏览器中突出显示文本,如查找文本【英文标题】:how to highlight text in python web browser like find text 【发布时间】:2019-02-26 11:43:08 【问题描述】:

我正在用 Python 和 PyQt5 开发一个 webview 浏览器,我想知道如何在 Python 和 PyQt5 网络浏览器中突出显示特定文本。我想在其他浏览器中突出显示特定文本,例如网络查找文本。

self.browser = QWebEngineView()
self.browser.HighlightAllOccurrences('hello world')

【问题讨论】:

【参考方案1】:

你必须使用QWebEngineView(或QWebEnginePage)的findText方法,在下面的例子中,当你按下Ctrl + F时会有一个搜索栏允许让用户更轻松地进行搜索:

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets

class SearchPanel(QtWidgets.QWidget):
    searched = QtCore.pyqtSignal(str, QtWebEngineWidgets.QWebEnginePage.FindFlag)
    closed = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(SearchPanel, self).__init__(parent)
        lay = QtWidgets.QHBoxLayout(self)
        done_button = QtWidgets.QPushButton('&Done')
        self.case_button = QtWidgets.QPushButton('Match &Case', checkable=True)
        next_button = QtWidgets.QPushButton('&Next')
        prev_button = QtWidgets.QPushButton('&Previous')
        self.search_le = QtWidgets.QLineEdit()
        self.setFocusProxy(self.search_le)
        done_button.clicked.connect(self.closed)
        next_button.clicked.connect(self.update_searching)
        prev_button.clicked.connect(self.on_preview_find)
        self.case_button.clicked.connect(self.update_searching)
        for btn in (self.case_button, self.search_le, next_button, prev_button, done_button, done_button):
            lay.addWidget(btn)
            if isinstance(btn, QtWidgets.QPushButton): btn.clicked.connect(self.setFocus)
        self.search_le.textChanged.connect(self.update_searching)
        self.search_le.returnPressed.connect(self.update_searching)
        self.closed.connect(self.search_le.clear)

        QtWidgets.QShortcut(QtGui.QKeySequence.FindNext, self, activated=next_button.animateClick)
        QtWidgets.QShortcut(QtGui.QKeySequence.FindPrevious, self, activated=prev_button.animateClick)
        QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self.search_le, activated=self.closed)

    @QtCore.pyqtSlot()
    def on_preview_find(self):
        self.update_searching(QtWebEngineWidgets.QWebEnginePage.FindBackward)

    @QtCore.pyqtSlot()
    def update_searching(self, direction=QtWebEngineWidgets.QWebEnginePage.FindFlag()):
        flag = direction
        if self.case_button.isChecked():
            flag |= QtWebEngineWidgets.QWebEnginePage.FindCaseSensitively
        self.searched.emit(self.search_le.text(), flag)

    def showEvent(self, event):
        super(SearchPanel, self).showEvent(event)
        self.setFocus(True)

class Browser(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Browser, self).__init__(parent)
        self._view = QtWebEngineWidgets.QWebEngineView()
        self.setCentralWidget(self._view)
        self._view.load(QtCore.QUrl('https://doc.qt.io/qt-5/qwebengineview.html'))
        self._search_panel = SearchPanel()
        self.search_toolbar = QtWidgets.QToolBar()
        self.search_toolbar.addWidget(self._search_panel)
        self.addToolBar(QtCore.Qt.BottomToolBarArea, self.search_toolbar)
        self.search_toolbar.hide()
        self._search_panel.searched.connect(self.on_searched)
        self._search_panel.closed.connect(self.search_toolbar.hide)
        self.create_menus()

    @QtCore.pyqtSlot(str, QtWebEngineWidgets.QWebEnginePage.FindFlag)
    def on_searched(self, text, flag):
        def callback(found):
            if text and not found:
                self.statusBar().show()
                self.statusBar().showMessage('Not found')
            else:
                self.statusBar().hide()
        self._view.findText(text, flag, callback)

    def create_menus(self):
        menubar = self.menuBar()
        file_menu = menubar.addMenu('&File')
        file_menu.addAction('&Find...', self.search_toolbar.show, shortcut=QtGui.QKeySequence.Find)

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    w = Browser()
    w.show()
    sys.exit(app.exec_())

【讨论】:

以上是关于如何在python网络浏览器中突出显示文本,如查找文本的主要内容,如果未能解决你的问题,请参考以下文章

如何更改浏览器搜索结果中突出显示文本的样式?

在选定 Excel 列的单元格中查找并突出显示重复的单元格和文本字符串

如何在移动 Safari 中使用 javascript 突出显示文本选择

突出显示文本功能,如 google play book

如何在android中为pdf查看器制作注释,如突出显示、删除线、下划线、绘制、添加文本等?

如何在 Visual Studio 中突出显示文本中出现的搜索词?