Skip to content
Related Articles

Related Articles

How to access textedit fields in pyqt?

View Discussion
Improve Article
Save Article
  • Last Updated : 05 Sep, 2022
View Discussion
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.

Python3




# 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.

Python3




# 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.

Python3




# Displaying the result in the second textEdit.
self.textEdit2.setPlainText(str(result)) 

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.

Python




# Importing PyQt
from PyQt5.QtWidgets import QApplication, QWidget,\
QTextEdit, QVBoxLayout, QPushButton
import sys
  
class TextEditDemo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
          
        # Setting the title of the GUI
        self.setWindowTitle("QTextEdit")  
          
        # 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
        layout.addWidget(self.textEdit1)
          
        # Adding the second textedit into the layout
        layout.addWidget(self.textEdit2)
          
        # Adding the button into the layout
        layout.addWidget(self.btnPress1)
  
        self.setLayout(layout)
  
        # Calling the function when the button is clicked
        self.btnPress1.clicked.connect(self.btnPress1_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()) + \
            10  
          
        # Displaying the result in the second textEdit.
        self.textEdit2.setPlainText(str(result))
  
  
if __name__ == '__main__':
    '''
    Main code that creates the GUI
    '''
    app = QApplication(sys.argv)
    win = TextEditDemo()
    win.show()
    sys.exit(app.exec_())

Output:

How to acces textedit fields pyqt?

GIF showing the output


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!