PyQt - 如何打开/关闭拼写检查 [英] PyQt - How to turn on/off spellchecking

查看:75
本文介绍了PyQt - 如何打开/关闭拼写检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下内容作为我的编辑器拼写检查器的基础.我想制作一个自动拼写检查按钮,按下该按钮将使拼写检查器工作,而未按下时,拼写检查器不应突出显示或建议任何内容.您知道一个类似于 LibreOffice/OpenOffice 自动拼写检查工具栏按钮的按钮吗?

I'm using the following as a basis for my editor's spellchecker. I want to make an auto-spellcheck button, which when pressed will make the spellchecker work, and when not pressed, the spellchecker should not highlight or suggest anything. You know the one similar to LibreOffice/OpenOffice auto-spelcheck toolbar button?

我可以让按钮工作,没问题.我的问题是我应该用荧光笔做什么?如果按下按钮时创建了荧光笔类,未按下按钮时该怎么办?杀死/破坏荧光笔类不起作用.

I can make the button work, no problem. My problem is what should I do with the highlighter? If the highlighter class is created when the button is pressed, what do I do when the button is not pressed? Killing/destroying the highlighter class doesn't work.

我找到了如何关闭上下文菜单:

I found how to turn the context menu off:

if self.actionSpellCheck.isChecked(): #This is my auto-spellchecking button
    popup_menu.exec_(event.globalPos())

好的,我知道了,要关闭突出显示,我只需要将荧光笔字典设置为无:

OK, I got it, to turn off highlighting I just need to set the highlighter dictionary to None:

self.highlighter.setDict(None)

并再次打开它,只需将字典设置回荧光笔:

and to turn it on again, just set the dictionary back to the highlighter:

self.highlighter.setDict(self.dict)

谢谢.

这是下面的原始代码,我在其中添加了一个带有按钮的工具栏:

Here's the original code below to which I added a toolbar with a button:

__license__ = 'MIT'
__copyright__ = '2009, John Schember '
__docformat__ = 'restructuredtext en'

import re
import sys

import enchant

from PyQt4.Qt import QAction
from PyQt4.Qt import QApplication
from PyQt4.Qt import QEvent
from PyQt4.Qt import QMenu
from PyQt4.Qt import QMouseEvent
from PyQt4.Qt import QPlainTextEdit
from PyQt4.Qt import QSyntaxHighlighter
from PyQt4.Qt import QTextCharFormat
from PyQt4.Qt import QTextCursor
from PyQt4.Qt import Qt
from PyQt4.QtCore import pyqtSignal

class SpellTextEdit(QPlainTextEdit):

    def __init__(self, *args):
        QPlainTextEdit.__init__(self, *args)

        # Default dictionary based on the current locale.
        self.dict = enchant.Dict("ru_RU")
        self.highlighter = Highlighter(self.document())
        self.highlighter.setDict(self.dict)

    def mousePressEvent(self, event):
        if event.button() == Qt.RightButton:
            # Rewrite the mouse event to a left button event so the cursor is
            # moved to the location of the pointer.
            event = QMouseEvent(QEvent.MouseButtonPress, event.pos(),
                Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
        QPlainTextEdit.mousePressEvent(self, event)

    def contextMenuEvent(self, event):
        popup_menu = self.createStandardContextMenu()

        # Select the word under the cursor.
        cursor = self.textCursor()
        cursor.select(QTextCursor.WordUnderCursor)
        self.setTextCursor(cursor)

        # Check if the selected word is misspelled and offer spelling
        # suggestions if it is.
        if self.textCursor().hasSelection():
            text = unicode(self.textCursor().selectedText())
            if not self.dict.check(text):
                spell_menu = QMenu('Spelling Suggestions')
                for word in self.dict.suggest(text):
                    action = SpellAction(word, spell_menu)
                    action.correct.connect(self.correctWord)
                    spell_menu.addAction(action)
                # Only add the spelling suggests to the menu if there are
                # suggestions.
                if len(spell_menu.actions()) != 0:
                    popup_menu.insertSeparator(popup_menu.actions()[0])
                    popup_menu.insertMenu(popup_menu.actions()[0], spell_menu)

        popup_menu.exec_(event.globalPos())

    def correctWord(self, word):
        '''
        Replaces the selected text with word.
        '''
        cursor = self.textCursor()
        cursor.beginEditBlock()

        cursor.removeSelectedText()
        cursor.insertText(word)

        cursor.endEditBlock()


class Highlighter(QSyntaxHighlighter):

    WORDS = u'(?iu)[\w\']+'

    def __init__(self, *args):
        QSyntaxHighlighter.__init__(self, *args)

        self.dict = None

    def setDict(self, dict):
        self.dict = dict

    def highlightBlock(self, text):
        if not self.dict:
            return

        text = unicode(text)

        format = QTextCharFormat()
        format.setUnderlineColor(Qt.red)
        format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)

        for word_object in re.finditer(self.WORDS, text):
            if not self.dict.check(word_object.group()):
                self.setFormat(word_object.start(),
                    word_object.end() - word_object.start(), format)


class SpellAction(QAction):

    '''
    A special QAction that returns the text in a signal.
    '''

    correct = pyqtSignal(unicode)

    def __init__(self, *args):
        QAction.__init__(self, *args)

        self.triggered.connect(lambda x: self.correct.emit(
            unicode(self.text())))


def main(args=sys.argv):
    app = QApplication(args)

    spellEdit = SpellTextEdit()
    spellEdit.show()

    return app.exec_()

if __name__ == '__main__':
    sys.exit(main())

推荐答案

您可以使用 setDocument 方法.

SpellTextEdit 类保留对其荧光笔的引用,因此只需添加如下几个方法:

The SpellTextEdit class keeps a reference to its highlighter, so just add a couple of methods like this:

def highlighterEnabled(self):
     return self.highlighter.document() is not None

def setHighlighterEnabled(self, enable):
    if enable != self.highlighterEnabled():
        if enable:
            self.highlighter.setDocument(self.document())
        else:
            self.highlighter.setDocument(None)

这篇关于PyQt - 如何打开/关闭拼写检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆