
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QTextBrowser
The term text browser is a web browser which rendered the text of the web pages. The text browser also ignored the graphical content.
In PyQt, QTextBrowser is a class which extend from another class named QTextEdit and adding the navigation functionality.
Users can follow links in hypertext documents. If you are looking to provide your users with an editable rich text editor, you can use QTextEdit. On the other hand, if you require a text browser without hypertext navigation, you can also use QTextEdit and use QTextEdit::setReadOnly() function to disable editing. Thus, if you only need to display a small piece of rich text, you can use QLabel.
The QTextBrowser class use the method setPlainText() to provide source text to the browser window.
Methods used in QTextBrowser
Following is the list of methods that can be used to build a text browser in PyQt −
- QTextBrowser() − This is a constructor method used to create a instance of a class QTextBrowser that provide a rich text browser and hypertext navigation.
- addwidget() − This add the QTextBrowser to the layout.
- setPlainText() − This allows user to write the text for browser window.
- setOpenExternalLinks(True) − This method enable the QTextBrowser to open the external link.
- loadText() − This method load the content to the text browser.
Example 1
In the following example, we display the text browser using the class QTextBrowser and its method. The group of text uses the method setPlainText() and addWidget() to call the main function of the program and display the output.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QTextBrowser, QVBoxLayout class TextBrowser(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QTextBrowser") # Set window size self.setGeometry(100, 100, 400, 300) # Create a vertical layout layout = QVBoxLayout(self) # Create a QTextBrowser self.text_browser = QTextBrowser(self) # Enable opening external links self.text_browser.setOpenExternalLinks(True) # Set some sample text (you can replace this with your own content) self.text_browser.setPlainText("Welcome to the QTextBrowser demo!\n\n" "This widget allows you to display rich text, " "including hyperlinks and formatting.") # Add the QTextBrowser to the layout layout.addWidget(self.text_browser) if __name__ == "__main__": app = QApplication(sys.argv) window = TextBrowser() window.show() sys.exit(app.exec())
Output
The above code produces the following output−

Example 2
Following is another example to illustrate the code snippet for text browser using QTextBrowser class and it's relevant methods.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QTextBrowser, QVBoxLayout class TextBrowserApp(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Text Browser') self.setGeometry(100, 100, 400, 300) layout = QVBoxLayout() self.text_browser = QTextBrowser() layout.addWidget(self.text_browser) self.setLayout(layout) # Load initial text self.loadText() def loadText(self): # Load text into the text browser self.text_browser.setPlainText("Welcome to the Text Browser!") if __name__ == '__main__': app = QApplication(sys.argv) window = TextBrowserApp() window.show() sys.exit(app.exec())
Output
The above code produces the following result−

Input text using QTextBrowser
Here, we have text widgets in Pyqt window that allows user to enter the text. Building of input text in QTextBrowser uses various method of PyQt −
- QLineEdit() − QLineEdit widget is a one-line text editor.
- QPushButton() − This method create a functional button while pressing on it.
- QVBoxLayout() − The QVBoxLayout is a class that constructs the layout container vertically.
- append() − Add the text to end.
- clear() − This method reset all the text from the browser window.
Example
In this example, we have two sets of vertical widget containers. The first container allows the user to enter the single-line text and when pressed to enter to keyboard it simply moves to the second container.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QTextBrowser, QPushButton, QVBoxLayout class MyApp(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.le = QLineEdit() self.le.returnPressed.connect(self.append_text) self.tab = QTextBrowser() self.tab.setAcceptRichText(True) self.tab.setOpenExternalLinks(True) self.clear_btn = QPushButton('Clear') self.clear_btn.pressed.connect(self.clear_text) vbox = QVBoxLayout() vbox.addWidget(self.le, 0) vbox.addWidget(self.tab, 1) vbox.addWidget(self.clear_btn, 2) self.setLayout(vbox) self.setWindowTitle('QTextBrowser') self.setGeometry(300, 300, 300, 300) self.show() def append_text(self): text = self.le.text() self.tab.append(text) self.le.clear() def clear_text(self): self.tab.clear() if __name__ == '__main__': app = QApplication(sys.argv) ex = MyApp() sys.exit(app.exec())
Output
The above code produces the following output−
