从 qtextedit 获取文本并将其分配给变量 [英] Get text from qtextedit and assign it to a variable

查看:76
本文介绍了从 qtextedit 获取文本并将其分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从使用 PyQt5 Designer 创建的 qtextedit 中获取文本时,出现错误或Python 停止工作"并且脚本自动停止.我尝试了多种解决方案,但没有任何效果.我必须将 qtextedit 中的文本分配给一个变量,以检查进程是否运行.这是 PyQt5 生成的代码:

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)

此处的按钮代码:

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("")

结束代码:

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_())

推荐答案

QTextEdit 没有任何 text() 方法,如果要获取文本必须使用toPlainText(),如果你想清理文本,最好使用clear(),因为它更易读.

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()

这篇关于从 qtextedit 获取文本并将其分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆