PyQt with Python GUI Programming Tutorial 13 – A Comprehensive Guide for Editors

Posted by


In this tutorial, we will continue to explore the Editor application and focus on implementing the search functionality using PyQt and Python. We will cover how to create a search bar, search through the text in the editor, and highlight the search results.

Let’s start by creating a new Python file and importing the necessary modules:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QVBoxLayout, QLineEdit, QPushButton
from PyQt5.QtGui import QTextCursor

Next, we will create a class for our main application window:

class Editor(QMainWindow):
    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('Editor')
        self.setGeometry(100, 100, 800, 600)

        self.text_edit = QTextEdit()
        self.search_bar = QLineEdit()
        self.search_btn = QPushButton('Search')

        layout = QVBoxLayout()
        layout.addWidget(self.search_bar)
        layout.addWidget(self.search_btn)
        layout.addWidget(self.text_edit)

        self.search_btn.clicked.connect(self.search_text)

        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def search_text(self):
        search_term = self.search_bar.text()
        cursor = self.text_edit.textCursor()
        cursor.select(QTextCursor.Document)

        if search_term:
            cursor = self.text_edit.document().find(search_term, cursor)
            if cursor.isNull():
                self.search_bar.setStyleSheet('background-color: #ff9999')
            else:
                self.search_bar.setStyleSheet('background-color: #ffffff')
                self.text_edit.setTextCursor(cursor)
                self.text_edit.ensureCursorVisible()

In the Editor class, we create a text editor widget, a search bar, a search button, and a layout to organize these widgets. We connect the clicked signal of the search button to the search_text method.

In the search_text method, we retrieve the search term from the search bar and use the find method to search through the text in the editor. If the search term is found, we set the text cursor to the found position and ensure that it is visible. If the search term is not found, we highlight the search bar to indicate that no results were found.

Finally, we create an instance of the Editor class and run the application:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    editor = Editor()
    editor.show()
    sys.exit(app.exec_())

Now, when you run the application, you will see a search bar at the top with a search button. You can enter a search term in the search bar and click the search button to highlight the text in the editor matching the search term.

This concludes our tutorial on implementing the search functionality in the Editor application using PyQt and Python. I hope you found this tutorial helpful and informative. Thank you for reading!

0 0 votes
Article Rating
13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@fernandofilipe1375
3 months ago

could you provide source code

@santoshkumarsah3730
3 months ago

Hello. Thanks for the great videos. Could you please define a function for the Find and Replace method if possible with QDialog or QDockWidget. I am new to PyQt and trying to learn designing a Text editor. Thanks in advance.

@sntshkmr60
3 months ago

Why is there '&' in menu name?

@cyansamuraigaming3099
3 months ago

How to blank the text Area

@carlfranz6805
3 months ago

I noticed that the Font and Color Picker didn't effect the Editor window at all. Is this how it's suppose to work?

@danieltauil5457
3 months ago

In this screen, how can I close just the Editor and back to first screen, but I don't wanna close everything and open again. Thxx!

@frikkievandermerwe
3 months ago

You are awesome! Thanks for these great videos.

@DREADL
3 months ago

Can anyone help me with a program I've been trying to make? I have actual code but I can't implement it into gui . I'll do Skype or email or anything that suits you.

@Kenwaldek
3 months ago

I have ported the lesson1 to lesson15 to PyQt5 you can find it https://github.com/kenwaldek/pythonprogramming

@yuyetasatcham6410
3 months ago

how i close the editor?

@Donygtr
3 months ago

What does the ampersand do in the string?

@david-nb5ug
3 months ago

Hi, thanks for the tutorial. Quick question why do you need to include "&Editor" ? in some of the past tutorials you use fileMenu = mainMenu.addMenu('&File'), extractAction = QtGui.QAction("&Menu Item", self) but you also use fontChoice = QtGui.QAction("Font", self). Just wondering what the & does.

Thanks.

@Viks4e70
3 months ago

How can I make some kind of a pop-up window so that the player writes his name in it and then save it to some high scores list?