Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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 PyQt4语法高亮拼写检查_Python_Pyqt4_Pyenchant - Fatal编程技术网

Python PyQt4语法高亮拼写检查

Python PyQt4语法高亮拼写检查,python,pyqt4,pyenchant,Python,Pyqt4,Pyenchant,我在对QTextEdit()实施附魔拼写检查时遇到问题。 我尝试: 我不能在QTextEdit中使用这个?如何将其用于self.text?如果我正确理解了您的问题,您只需执行以下操作: from PyQt4 import QtGui from enchant.checker import SpellChecker import re import sys class Main(QtGui.QMainWindow): def __init__(self, parent = None):

我在对QTextEdit()实施附魔拼写检查时遇到问题。 我尝试:


我不能在QTextEdit中使用这个?如何将其用于self.text?

如果我正确理解了您的问题,您只需执行以下操作:

from PyQt4 import QtGui
from enchant.checker import SpellChecker
import re
import sys

class Main(QtGui.QMainWindow):

    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self,parent)
        self.initUI()

    def initToolbar(self):
        self.spellcheckAction = QtGui.QAction(QtGui.QIcon("icons/logo.png"),"Spellcheck",self)
        self.spellcheckAction.setStatusTip("Spell Check document")
        self.spellcheckAction.setShortcut("Ctrl+S")
        self.spellcheckAction.triggered.connect(self.spellcheckHandler)
        self.toolbar = self.addToolBar("Options")
        self.toolbar.addAction(self.spellcheckAction)

    def initUI(self):
        self.text = QtGui.QTextEdit(self)
        self.initToolbar()
        self.text.setTabStopWidth(33)
        self.setCentralWidget(self.text)
        self.setGeometry(100,100,1030,800)
        self.setWindowTitle("Writer")
        self.setWindowIcon(QtGui.QIcon("icons/icon.png"))

    def spellcheckHandler(self):
        chkr = SpellChecker("en_US")
        s = str(self.text.toPlainText())
        chkr.set_text(s)
        for err in chkr:
            self.replaceAll(err.word)

    def find(self, query):
        text = self.text.toPlainText()
        query = r'\b' + query + r'\b'
        flags = 0 
        pattern = re.compile(query,flags)
        start = self.lastMatch.start() + 1 if self.lastMatch else 0
        self.lastMatch = pattern.search(text,start)
        if self.lastMatch:
            start = self.lastMatch.start()
            end = self.lastMatch.end()
            self.moveCursor(start,end)
        else:
            self.text.moveCursor(QtGui.QTextCursor.End)

    def replace(self):
        cursor = self.text.textCursor()
        if self.lastMatch and cursor.hasSelection():
            self.text.setTextBackgroundColor(QtGui.QColor(0,255,0))
            self.text.setTextCursor(cursor)

    def replaceAll(self, query):
        self.lastMatch = None
        self.find(query)
        while self.lastMatch:
            self.replace()
            self.find(query)

    def moveCursor(self,start,end):
        cursor = self.text.textCursor()
        cursor.setPosition(start)
        cursor.movePosition(QtGui.QTextCursor.Right,QtGui.QTextCursor.KeepAnchor,end - start)
        self.text.setTextCursor(cursor)

def main():
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
要获得建议,请使用

chkr.suggest(err.word).

如果我正确理解了你的问题,你只需要做如下事情:

from PyQt4 import QtGui
from enchant.checker import SpellChecker
import re
import sys

class Main(QtGui.QMainWindow):

    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self,parent)
        self.initUI()

    def initToolbar(self):
        self.spellcheckAction = QtGui.QAction(QtGui.QIcon("icons/logo.png"),"Spellcheck",self)
        self.spellcheckAction.setStatusTip("Spell Check document")
        self.spellcheckAction.setShortcut("Ctrl+S")
        self.spellcheckAction.triggered.connect(self.spellcheckHandler)
        self.toolbar = self.addToolBar("Options")
        self.toolbar.addAction(self.spellcheckAction)

    def initUI(self):
        self.text = QtGui.QTextEdit(self)
        self.initToolbar()
        self.text.setTabStopWidth(33)
        self.setCentralWidget(self.text)
        self.setGeometry(100,100,1030,800)
        self.setWindowTitle("Writer")
        self.setWindowIcon(QtGui.QIcon("icons/icon.png"))

    def spellcheckHandler(self):
        chkr = SpellChecker("en_US")
        s = str(self.text.toPlainText())
        chkr.set_text(s)
        for err in chkr:
            self.replaceAll(err.word)

    def find(self, query):
        text = self.text.toPlainText()
        query = r'\b' + query + r'\b'
        flags = 0 
        pattern = re.compile(query,flags)
        start = self.lastMatch.start() + 1 if self.lastMatch else 0
        self.lastMatch = pattern.search(text,start)
        if self.lastMatch:
            start = self.lastMatch.start()
            end = self.lastMatch.end()
            self.moveCursor(start,end)
        else:
            self.text.moveCursor(QtGui.QTextCursor.End)

    def replace(self):
        cursor = self.text.textCursor()
        if self.lastMatch and cursor.hasSelection():
            self.text.setTextBackgroundColor(QtGui.QColor(0,255,0))
            self.text.setTextCursor(cursor)

    def replaceAll(self, query):
        self.lastMatch = None
        self.find(query)
        while self.lastMatch:
            self.replace()
            self.find(query)

    def moveCursor(self,start,end):
        cursor = self.text.textCursor()
        cursor.setPosition(start)
        cursor.movePosition(QtGui.QTextCursor.Right,QtGui.QTextCursor.KeepAnchor,end - start)
        self.text.setTextCursor(cursor)

def main():
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
要获得建议,请使用

chkr.suggest(err.word).