python – 从qtextedit获取文本并将其分配给变量
作者:互联网
当我尝试从使用PyQt5 Designer创建的qtextedit获取文本时,我收到错误或“Python停止工作”并且脚本自动停止.我尝试了多种解决方案,但没有任何效果.我必须将qtextedit中的文本分配给变量,以检查进程是否运行.这是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)
这里是从文本框中获取文本并分配给变量的代码:
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(),因为它使它更具可读性.
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,pyqt5,python,pyqt 来源: https://codeday.me/bug/20190929/1833647.html