Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import re
- from PyQt4.QtGui import *
- from PyQt4.QtCore import *
- class Window(QWidget):
- def __init__(self):
- QWidget.__init__(self)
- self.patIsWord = re.compile('\w+')
- vbox = QVBoxLayout(self)
- self.text = QPlainTextEdit()
- self.text.setPlainText('Esto es una prueba\nprueba de ejemplo\nnada mas que decir')
- vbox.addWidget(self.text)
- self.connect(self.text, SIGNAL("cursorPositionChanged()"), self.highlight_current_word)
- def text_under_cursor(self):
- tc = self.text.textCursor()
- tc.select(QTextCursor.WordUnderCursor)
- return tc.selectedText()
- def highlight_current_word(self):
- self.extraSelections = []
- word = self.text_under_cursor()
- if self.patIsWord.match(word):
- lineColor = QColor(Qt.red)
- lineColor.setAlpha(100)
- cursor = self.text.document().find(word)
- block = self.text.firstVisibleBlock()
- while block.isValid():
- selection = QTextEdit.ExtraSelection()
- selection.format.setBackground(lineColor)
- selection.format.setToolTip(word)
- selection.cursor = cursor
- self.extraSelections.append(selection)
- cursor = self.text.document().find(word, cursor.position())
- block = block.next()
- self.text.setExtraSelections(self.extraSelections)
- app = QApplication(sys.argv)
- w = Window()
- w.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement