from PySide6.QtWidgets import QApplication,QMainWindow
from PySide6.QtCore import QThread
from PySide6 import QtCore, QtGui, QtWidgets
import io
import matlab.engine
import sys
import time
#GUI is generated
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(342, 265)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.MatlabConsoleOutTextEdit = QtWidgets.QTextEdit(self.centralwidget)
self.MatlabConsoleOutTextEdit.setObjectName("MatlabConsoleOutTextEdit")
self.verticalLayout.addWidget(self.MatlabConsoleOutTextEdit)
self.StartMatlabButton = QtWidgets.QPushButton(self.centralwidget)
self.StartMatlabButton.setObjectName("StartMatlabButton")
self.verticalLayout.addWidget(self.StartMatlabButton)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 342, 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 retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.StartMatlabButton.setText(_translate("MainWindow", "StartMatlabButton"))
#Run Matlab processing in separate QThread
class matlabProcThread(QThread):
#create signal to detect finished thread
finished = QtCore.Signal()
#inherit data from GUI to be available for this class
def __init__(self,parent):
super().__init__()
self.parent = parent
def run(self):
#Start Matlab engine
self.eng = matlab.engine.start_matlab()
self.eng.desktop(nargout=0)
#create matlab structure with options for data processing
opt = {}
#data directory
opt['folder'] = 'C:\\Users'
#Nominal bus voltage
opt['Vn'] = 13.2
#Nominal fundamental frequency
opt['F1nom'] = 60.0
#System frequency
opt['F1'] = 59.87
####################################################################################################################################################
###################################################### Matlab Processing and data progress printing ################################################
####################################################################################################################################################
out = io.StringIO()
err = io.StringIO()
#Run the data processing script wrapper that processes options and runs data processing script
future = self.eng.DataProc(opt,stdout=out,stderr=err,nargout=0,background=True)
#Show data processing progress in QTextEdit while the data processing script is running
future.result()
while not future.done():
self.matlabConsoleOutputWrite(out.getvalue())
time.sleep(1)
self.eng.quit()
self.finished.emit()
####################################################################################################################################################
####################################################################################################################################################
####################################################################################################################################################
#function to write text to the console output text field
def matlabConsoleOutputWrite(self, text):
cursor = self.parent.MatlabConsoleOutTextEdit.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.parent.MatlabConsoleOutTextEdit.setTextCursor(cursor)
self.parent.MatlabConsoleOutTextEdit.ensureCursorVisible()
#Run GUI process
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.setupUi(self) #generate GUI
#Start Matlab data processing when button is clicked
self.StartMatlabButton.clicked.connect(self.startProcessing)
def startProcessing(self):
self.statusBar().showMessage("Processing Data",0)
#Create thread
self.ProcessThread = matlabProcThread(self)
#Enable termination
self.ProcessThread.setTerminationEnabled(True)
#Delete contents after finishing
self.ProcessThread.finished.connect(self.ProcessThread.deleteLater)
#Start thread
self.ProcessThread.start()
#Disable start scan button
self.StartMatlabButton.setEnabled(False)
#Enable start scan button again after thread finishes
self.ProcessThread.finished.connect(lambda: self.StartMatlabButton.setEnabled(True))
self.statusBar().clearMessage()
if __name__ == '__main__':
#initiate app instance
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
Gui = MainWindow()
Gui.show()
sys.exit(app.exec())