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!
could you provide source code
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.
Why is there '&' in menu name?
How to blank the text Area
I noticed that the Font and Color Picker didn't effect the Editor window at all. Is this how it's suppose to work?
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!
You are awesome! Thanks for these great videos.
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.
I have ported the lesson1 to lesson15 to PyQt5 you can find it https://github.com/kenwaldek/pythonprogramming
how i close the editor?
What does the ampersand do in the string?
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.
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?