如何解决在运行PyQt5 GUI显示其他窗口后,为什么会出现RuntimeError:QPlainTextEdit类型的包装C / C ++对象已被删除的原因
我试图让用户从GUI中选择要执行的过程。没有GUI部分,我可以执行这些过程,并且它们都同时出现在可以独立运行的单独窗口上。现在,我希望用户从GUI中选择首先运行哪个过程,以避免同时打开多个窗口。当我选择第二个按钮时程序正常执行时,出现以下错误消息: RuntimeError:QPlainTextEdit类型的包装C / C ++对象已被删除 。我已经解决了这个问题,并尝试了所提出的建议,但没有成功。在从 [https://stackoverflow.com/questions/63302430/call-different-window-classes-into-a-main-window-in-pqt5-python],我试图尽量减少代码。尽管使用了某些特定的程序包,但这些程序包不负责观察到的错误,任何人都应该能够运行此代码。 Mainwindow类已经在使用Qt来显示窗口,但是当独立使用时,我没有任何错误。该错误是Ui_MainWindow(object)类所固有的,我也尝试使用项目列表进行相同的问题。选择“运行2”过程时,每次按“队列”时都会出现此错误。
import logging
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
import sys
import random
import pandas as pd
from pymeasure.log import console_log
from pymeasure.display.Qt import QtGui
from PyQt5 import QtCore,QtGui,QtWidgets
from pymeasure.display.windows import ManagedWindow
from pymeasure.experiment import Procedure,Results
from pymeasure.experiment import IntegerParameter,FloatParameter,Parameter
import tempfile
# this class uses the Pymeasure package to run and display experiments here it s just generating a random number. the iterations
#number being the input parameter on the window and one can queue several runs
# execute is the method generating the random number and Mainwindow is the dipsplay and the queue of the runs. the data are
# save in a temp file
class RandomProcedure_1(Procedure):
iterations = IntegerParameter('Loop Iterations',default=10)
DATA_COLUMNS = ['Iteration','Random Number']
#
def startup(self):
log.info("Setting the time of the random number generator")
random.seed(self.seed)
def execute(self):
log.info("Starting the loop of %d iterations" % self.iterations)
for i in range(self.iterations):
data = {
'Iteration': i,'Random Number': random.randint(1,50)
}
self.emit('results',data)
log.debug("Emitting results: %s" % data)
if self.should_stop():
log.warning("Caught the stop flag in the procedure")
break
class MainWindow_1(ManagedWindow):
# this define the window where the random number vs iteration plot is displayed
def __init__(self):
super(MainWindow_1,self).__init__(
procedure_class=RandomProcedure_1,inputs=['iterations'],displays=['iterations'],x_axis='Iteration',y_axis='Random Number',)
self.setWindowTitle('RandomProcedure_1')
def queue(self,*,procedure=None):
if procedure is None:
procedure = self.make_procedure()
filename = tempfile.mktemp()
log.info("Constructing the Results with a data file: %s" % filename)
results = Results(procedure,filename)
experiment = self.new_experiment(results)
self.manager.queue(experiment)
# the GUI with 2 pushbuttons for each procedure (here the same duplicated for simplicity)
# i also tried with a list of item but the same error occurs
class Ui_MainWindow(object):
def setupUi(self,MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800,600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110,160,112,34))
self.pushButton.setObjectName("EXP1")
self.pushButton.clicked.connect(self.startProcedure_1)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(580,170,34))
self.pushButton_2.setObjectName("EXP2")
self.pushButton_2.clicked.connect(self.startProcedure_1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0,800,21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def startProcedure_1(self):
self.window = MainWindow_1()
self.window.show()
def retranslateUi(self,MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow","Procedures"))
self.pushButton.setText(_translate("MainWindow","Run 1"))
self.pushButton_2.setText(_translate("MainWindow","Run 2"))
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_())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。