Skip to content
Related Articles
Open in App
Not now

Related Articles

How to access textedit fields in pyqt?

Improve Article
Save Article
  • Last Updated : 05 Sep, 2022
Improve Article
Save Article

TextEdit is a widget in PyQt5 that can be used to take input from the user and display the data to the user. It can take single-line text or multiline texts. A vertical scroll bar appears if the text does not fit into the widget. Here, we will create two TextEdit fields and a button. In the first TextEdit field, we will enter a number, and then on clicking the button, 10 will be added to that number and the result will be displayed in the second TextEdit field. TextEdit widget is provided by PyQt5 that can be installed in python using the following command. 

Required Package:

pip install PyQt5

Implementation: Stepwise 

Step 1: Let us first create the two TextEdit fields using PyQt5.


# Create first TextEdit where we
# will enter a number.
self.textEdit1 = QTextEdit()
# Create second TextEdit where 
# result will shown.
self.textEdit2 = QTextEdit()
# Create a button
# when clicked, result will be shown.
self.btnPress1 = QPushButton("Button 1")

Step 2: To access the number in the first TextEdit and add 10 to it, we will use the following command.


# Reading the data from the first
# textEdit and adding 10 to it
result = self.textEdit1.toPlainText()+10

Step 3: To put this result in the second TextEdit, we will use the following command.


# Displaying the result in the second textEdit.

Step 4: The above line of code will display the updated result in the second TextEdit.

How to acces textedit fields pyqt?

Code Output

Code Implementation

Here, we are importing PyQt and creating a class TextEditDemo in which we are setting the size of a window and creating the two TextEdit boxes one is for taking an input and another is for showing a result after you click on a button. The result will be always adding 10 to the input number.


# Importing PyQt
from PyQt5.QtWidgets import QApplication, QWidget,\
QTextEdit, QVBoxLayout, QPushButton
import sys
class TextEditDemo(QWidget):
    def __init__(self, parent=None):
        # Setting the title of the GUI
        # Setting the size of the window
        self.resize(300, 270)  
        # Creating the first textedit
        self.textEdit1 = QTextEdit()  
        # Creating the second textedit
        self.textEdit2 = QTextEdit() 
        # Creating the button
        self.btnPress1 = QPushButton("Button 1")  
        # Creating a vertical box layout
        layout = QVBoxLayout()  
        # Adding the first textedit into the layout
        # Adding the second textedit into the layout
        # Adding the button into the layout
        # Calling the function when the button is clicked
    def btnPress1_Clicked(self):
        This function reads the data from the first TextEdit and
        add 10 to it. After adding the number, it displays the result
        in the second TextEdid.
        result = int(self.textEdit1.toPlainText()) + \
        # Displaying the result in the second textEdit.
if __name__ == '__main__':
    Main code that creates the GUI
    app = QApplication(sys.argv)
    win = TextEditDemo()


How to acces textedit fields pyqt?

GIF showing the output

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!