Search code examples
pythonpyqt5qt5

select and highlight the next word from QTextEdit on button click and show in QLabel


The aim is to select and highlight the next word on each click of QPushButton and shown that selected/highlighted word in the QLabel as well.

The minimum working code of my Python pyqt5 gui application is given below along with its output

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QTextEdit,QVBoxLayout,QPushButton,QLabel
import lorem
s=lorem.sentence()
p=lorem.paragraph()
t=lorem.text()

class myWindow(QMainWindow):
    def __init__(self):
        super(myWindow,self).__init__()
        self.setWindowTitle('application')
        self.setGeometry(200,200,800,600)
        self.initUI()

    def initUI(self):
        # Cannot set QxxLayout directly on the QMainWindow
        # Need to create a QWidget and set it as the central widget
        widget = QWidget()
        layout=QVBoxLayout()
        #self.setLayout(layout)
        self.textbox=QTextEdit(self)
        #self.textbox.setGeometry(20,20,750,520)
        self.textbox.setText(t)
        layout.addWidget(self.textbox)

        self.l1=QLabel(self)
        #self.l1.setGeometry(260,560,20,20)
        self.l1.setText('highlited word')
        self.l1.adjustSize()
        layout.addWidget(self.l1)
        
        self.b1=QPushButton('next word',self)
        #self.b1.move(460,550)
        layout.addWidget(self.b1)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

def mymain():
    app=QApplication(sys.argv)
    win=myWindow()
    win.show()
    sys.exit(app.exec_())

if __name__=='__main__':
    mymain()

and the output is

The output of the above code

What I would like is: When the QPushButton is clicked, the first word of the text in QTextEdit box is selected and highlited and is also shown on the QLabel, and when the QPushButton is pressed again, the second word is selected/highlighted in QTextEdit box and is shown on the QLabel box, and the process continues.

Note: This is different from 'Find' as I have to go though all words in the text with each click.


Solution

  • Try QTextCursor.movePosition(QTextCursor.NextWord, QTextCursor.KeepAnchor).

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QTextEdit,QVBoxLayout,QPushButton,QLabel
    import lorem
    s=lorem.sentence()
    p=lorem.paragraph()
    t=lorem.text()
    
    class myWindow(QMainWindow):
        def __init__(self):
            super(myWindow,self).__init__()
            self.setWindowTitle('application')
            self.setGeometry(200,200,800,600)
            self.initUI()
    
        def initUI(self):
            # Cannot set QxxLayout directly on the QMainWindow
            # Need to create a QWidget and set it as the central widget
            widget = QWidget()
            layout=QVBoxLayout()
            #self.setLayout(layout)
            self.textbox=QTextEdit(self)
            #self.textbox.setGeometry(20,20,750,520)
            self.textbox.setText(t)
    
            # Move Cursor to Start of the document.
            cursor = self.textbox.textCursor()
            cursor.movePosition(cursor.Start)
    
            layout.addWidget(self.textbox)
    
            self.l1=QLabel(self)
            #self.l1.setGeometry(260,560,20,20)
            self.l1.setText('highlited word')
            self.l1.adjustSize()
            layout.addWidget(self.l1)
            
            self.b1=QPushButton('next word',self)
            self.b1.clicked.connect(self.next_word)
            #self.b1.move(460,550)
            layout.addWidget(self.b1)
            widget.setLayout(layout)
            self.setCentralWidget(widget)
    
        def next_word(self):
            word = self.get_next_word()
            while(word in [".", ""]): #Filter
                word = self.get_next_word()
            self.l1.setText(word)
    
        def get_next_word(self):
            cursor = self.textbox.textCursor()
            if(cursor.atEnd()):
                return "End Of document!"
            cursor.clearSelection()
            cursor.movePosition(cursor.NextWord, cursor.KeepAnchor)
            self.textbox.setTextCursor(cursor)
            return cursor.selectedText()        
    
    
    def mymain():
        app=QApplication(sys.argv)
        win=myWindow()
        win.show()
        sys.exit(app.exec_())
    
    if __name__=='__main__':
        mymain()