Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt-如何打开/关闭拼写检查_Python_Pyqt_Syntax Highlighting_Pyqt4_Spell Checking - Fatal编程技术网

Python PyQt-如何打开/关闭拼写检查

Python PyQt-如何打开/关闭拼写检查,python,pyqt,syntax-highlighting,pyqt4,spell-checking,Python,Pyqt,Syntax Highlighting,Pyqt4,Spell Checking,我使用以下内容作为编辑拼写检查的基础。我想做一个自动拼写检查按钮,按下该按钮将使拼写检查器工作,不按下时,拼写检查器不应突出显示或建议任何内容。你知道一个类似于LibreOffice/OpenOffice自动spelcheck工具栏按钮的按钮吗 我可以让按钮工作,没问题。我的问题是我该怎么处理荧光灯?如果highlighter类是在按下按钮时创建的,那么在未按下按钮时该怎么办?杀死/摧毁highlighter类不起作用 我找到了如何关闭关联菜单: if self.actionSpellCheck

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

我可以让按钮工作,没问题。我的问题是我该怎么处理荧光灯?如果highlighter类是在按下按钮时创建的,那么在未按下按钮时该怎么办?杀死/摧毁highlighter类不起作用

我找到了如何关闭关联菜单:

if self.actionSpellCheck.isChecked(): #This is my auto-spellchecking button
    popup_menu.exec_(event.globalPos())
好的,我知道了,要关闭高亮显示,我只需要将高亮字典设置为“无”:

self.highlighter.setDict(None)
要再次打开,只需将字典设置回荧光灯:

self.highlighter.setDict(self.dict)
多谢各位

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

__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())

您可以使用highlighter的方法启用和禁用语法highlighter

SpellTextEdit
类保留了对其highlighter的引用,因此只需添加两个类似以下的方法:

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)