L
L
Lolec3222021-03-30 23:54:51
Python
Lolec322, 2021-03-30 23:54:51

How to output text from QTextEdit to a variable?

Hello! I'm trying to figure out how to output text from the editor to a variable, the program window starts, but after clicking the button (calling the function), the following error occurs: AttributeError: 'bool' object has no attribute 'textEdit' I'm

attaching the code:

import sys
from PyQt5.QtWidgets import *

class Example(QWidget):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("Пример")


        self.textEdit = QTextEdit(self)
        self.textEdit.toPlainText()
        
        self.pushButton = QPushButton('ok', self)
        self.pushButton.move(50, 50)
        self.pushButton.clicked.connect(click)
      
         
def click(self):
    text = self.textEdit.toPlainText()
    print(text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Example()
    form.show()
    app.exec()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-03-31
@Lolec322

import sys
from PyQt5.QtWidgets import *

class Example(QWidget):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("Пример")
        
        self.grid_layout = QGridLayout()
        
        self.setLayout(self.grid_layout)

        self.textEdit = QTextEdit(self)
        self.textEdit.toPlainText()
        
        self.pushButton = QPushButton('ok', self)
        self.pushButton.move(50, 50)
        self.pushButton.clicked.connect(self.pushButtonClickedHandler)
        
        self.grid_layout.addWidget(self.textEdit, 0, 0)
        self.grid_layout.addWidget(self.pushButton)
    def pushButtonClickedHandler(self):
        text = self.textEdit.toPlainText()
        
        self.grid_layout.addWidget(QLabel(text = text))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Example()
    form.show()
    app.exec()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question