Get text from qtextedit and assign it to a variable
Asked Answered
C

1

22

When I try to get the text from the qtextedit created with PyQt5 Designer I get an error or "Python stop working" and the script stop automatically. I've tried multiple solutions but nothing works. I have to assign the text from the qtextedit to a variable, for check if the process run or no. That's the code generated by PyQt5:

from PyQt5 import QtCore, QtGui, QtWidgets
import psutil
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(221, 157)
        MainWindow.setMinimumSize(QtCore.QSize(221, 157))
        MainWindow.setMaximumSize(QtCore.QSize(221, 157))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.cercaprocesso = QtWidgets.QPushButton(self.centralwidget)
        self.cercaprocesso.setGeometry(QtCore.QRect(0, 80, 221, 31))
        font = QtGui.QFont()
        font.setFamily("MS Shell Dlg 2")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.cercaprocesso.setFont(font)
        self.cercaprocesso.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(0, 0, 221, 40))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.textbox = QtWidgets.QTextEdit(self.centralwidget)
        self.textbox.setGeometry(QtCore.QRect(30, 40, 161, 31))
        self.textbox.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.textbox.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 221, 21))
        self.menubar.setObjectName("menubar")
        self.menucredit = QtWidgets.QMenu(self.menubar)
        self.menucredit.setObjectName("menucredit")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionbot_created_by_andrea1980345_inforge_user = QtWidgets.QAction(MainWindow)     
        self.actionbot_created_by_andrea1980345_inforge_user.setObjectName ("actionbot")
        self.menucredit.addAction(self.actionbot_created_by_andrea1980345_inforge_user)
        self.menubar.addAction(self.menucredit.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

Button Code here:

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "andrea1980345 Bot"))
    self.cercaprocesso.setText(_translate("MainWindow", "Cerca Processo"))
    self.label.setText(_translate("MainWindow", "Inserire il nome del processo"))
    self.menucredit.setTitle(_translate("MainWindow", "Credit"))
    self.actionbot_created_by_andrea1980345_inforge_user.setText(_translate("MainWindow", "bot"))
    self.cercaprocesso.clicked.connect(self.search)

Here the code for get the text from the textbox and assign to a variable:

def search(self):
    textboxValue = self.textbox.text()
    for pid in psutil.pids():  # Controlla se il processo è attivo
        listapid = psutil.Process(pid)
        if listapid.name() == textboxValue:
            print('Processo trovato')
    self.textbox.setText("")

End code:

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Commonalty answered 13/3, 2018 at 20:24 Comment(2)
Can you provide an output of the error so we can see where it's triggered?Imperialism
I get "Python stop Working" with this one.Commonalty
K
40

QTextEdit does not have any text() method, if you want to get the text you must use toPlainText(), if you want to clean the text it is better to use clear() since it makes it more readable.

def search(self):
    textboxValue = self.textbox.toPlainText()
    for pid in psutil.pids():  # Controlla se il processo è attivo
        listapid = psutil.Process(pid)
        if listapid.name() == textboxValue:
            print('Processo trovato')
    self.textbox.clear()
Kowal answered 13/3, 2018 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.