Tengo un ejecutable que me gustaría integrar en mi interfaz. Por el momento tengo que se pueda ejecutar con un botón pero la ventana de la consola de comandos con la ejecución me gustaría poder integrarla en mi interfaz.
He probado una solución que hay en inglés pero no me ha funcionado: https://stackoverflow.com/questions/41474647/run-a-foreign-exe-inside-a-python-gui-pyqt
Código:
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
import subprocess, time, win32gui, csv, os
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
"""
#names: b - button
tb - toolbar
lb - label
"""
# main window
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.iniUI()
# init main window
def iniUI(self):
# dimensions
self.setGeometry(QtCore.QRect(350, 175, 500, 350))
self.setWindowTitle("SIM_DDWs")
self.setWindowIcon(QtGui.QIcon('logo.png'))
self.center()
# methods
self.menu()
# buttons
"""
# button load configuration
bLoadConfig = QtWidgets.QPushButton('Load Configuration', self)
bLoadConfig.resize(bLoadConfig.minimumSizeHint())
bLoadConfig.move(370, 210)
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
bLoadConfig.setToolTip('Load an existing configuration for the simulator')
"""
# button view results
bViewResult = QtWidgets.QPushButton('View Results', self)
bViewResult.clicked.connect(self.openNewWindowPlot)
bViewResult.resize(bViewResult.minimumSizeHint())
bViewResult.move(370, 255)
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
bViewResult.setToolTip('View the results')
# button exit
bExit = QtWidgets.QPushButton("Exit", self)
bExit.clicked.connect(self.close)
bExit.resize(bViewResult.minimumSizeHint())
bExit.move(370, 300)
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
bExit.setToolTip('Exit the app and go to the desktop')
#button run
bRun = QtWidgets.QPushButton('', self)
bRun.clicked.connect(self.runExe)
bRun.resize(bViewResult.minimumSizeHint())
bRun.move(370, 210)
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
bRun.setToolTip('Run de app')
bRun.setIcon(QtGui.QIcon('run.png'))
"""
# button built configuration
bBuiltConfig = QtWidgets.QPushButton('Built Configuration', self)
bBuiltConfig.resize(bLoadConfig.minimumSizeHint())
bBuiltConfig.move(370, 125)
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
bBuiltConfig.setToolTip('Built a new configuration for the simulator')
"""
# display app
self.show()
# menu
def menu(self):
mainMenu = self.menuBar()
tbFile = mainMenu.addMenu('File')
# add run to toolbar
tbRun = QtWidgets.QAction(QtGui.QIcon('run.png'), 'Run', self)
tbRun.setShortcut('F5')
tbRun.setStatusTip('Run')
tbRun.triggered.connect(self.runExe)
tbFile.addAction(tbRun)
"""
# add built config to toolbar
tbBuilt = QtWidgets.QAction(QtGui.QIcon('built.png'),'Built Configuration', self)
tbFile.addAction(tbBuilt)
# add load config to toolbar
tbLoad = QtWidgets.QAction(QtGui.QIcon('load.png'), 'Load Configuration', self)
tbFile.addAction(tbLoad)
"""
# add view results to toolbar
tbView = QtWidgets.QAction(QtGui.QIcon('results.png'), 'View Results', self)
tbView.triggered.connect(self.openNewWindowPlot)
tbFile.addAction(tbView)
# add exit to toolbar
tbExit = QtWidgets.QAction(QtGui.QIcon('exit.png'), 'Exit', self)
tbExit.setShortcut('Ctrl+Q')
tbExit.setStatusTip('Exit')
tbExit.triggered.connect(self.close)
tbFile.addAction(tbExit)
# add editor to toolbar
tbEditor = QtWidgets.QAction(QtGui.QIcon('editor.png'), 'Editor', self)
tbEditor.setShortcut('Ctrl+E')
tbEditor.setStatusTip('Editor')
tbEditor.triggered.connect(self.openNewWindow)
editorMenu = mainMenu.addMenu('Editor')
editorMenu.addAction(tbEditor)
# add help to toolbar
tbHelp = mainMenu.addMenu('Help')
tbHelp2 = QtWidgets.QAction(QtGui.QIcon('help.png'), 'Help', self)
tbHelp2.setStatusTip('Help')
tbHelp2.triggered.connect(self.close)
tbHelp.addAction(tbHelp2)
# add about to toolbar
tbAbout = QtWidgets.QAction(QtGui.QIcon('about.png'), 'About', self)
tbAbout.setStatusTip('About')
tbAbout.triggered.connect(self.helpWindow)
tbHelp.addAction(tbAbout)
# help window
def helpWindow(self):
widget = QtWidgets.QDialog()
widget.setWindowTitle('About')
widget.setWindowIcon(QtGui.QIcon('about.png'))
widget.exec_()
widget.show()
# question if you want to close it or not
def closeEvent(self, event):
reply = QtWidgets.QMessageBox.question(self, 'Exit', "Are you sure you want to exit the application?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
# center the app
def center(self):
cent = self.frameGeometry()
center = QtWidgets.QDesktopWidget().availableGeometry().center()
cent.moveCenter(center)
self.move(cent.topLeft())
# open new window
def openNewWindow(self):
self.neww = newWindow()
self.neww.show()
def openNewWindowPlot(self):
self.newWindowPlot = newPlotWindow()
self.newWindowPlot.show()
# run the main program in C++
def runExe(self):
exePath = ('main.exe')
subprocess.Popen(exePath)
hwnd = win32gui.FindWindowEx(0, 0, "mainFrame", 'main.exe')
time.sleep(0.05)
window = QtGui.QWindow.fromWinId(hwnd)
self.createWindowContainer(window, self)
self.setGeometry(350, 175, 500, 350)
self.center()
self.setWindowTitle('Main')
window.show()
class newPlotWindow(QtWidgets.QMainWindow):
def __init__(self):
super(newPlotWindow, self).__init__()
self.setGeometry(600, 600, 1000, 600)
self.center()
self.setWindowTitle('Results')
self.setWindowIcon(QtGui.QIcon('results.png'))
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
self.addToolBar(self.toolbar)
self.button = QtWidgets.QPushButton('Plot')
self.button.clicked.connect(self.readCSV)
self.main_widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.main_widget)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.main_widget.setLayout(layout)
# center the app
def center(self):
cent = self.frameGeometry()
center = QtWidgets.QDesktopWidget().availableGeometry().center()
cent.moveCenter(center)
self.move(cent.topLeft())
# read csv file generated by main and print the plot
def readCSV(self):
with open('resultado.csv') as csvFile:
try:
reader = csv.DictReader(csvFile, delimiter=';')
toa, frecIni = [], []
for row in reader:
# print(row["TOA (ns)"], row["Frecuencia Inicial (MHz)"])
toa.append(row["TOA (ns)"])
frecIni.append(row["Frecuencia Inicial (MHz)"])
ax = self.figure.add_subplot(111)
ax.clear()
toaf = [float(i) for i in toa]
frecInif = [float(i) for i in frecIni]
ax.plot(toaf, frecInif, "ro")
ax.set_xlabel('TOA (ns)')
ax.set_ylabel('Frecuencia Inicial (MHz)')
self.canvas.draw()
except Exception as e:
print("Error:", e)
# new window
class newWindow(QtWidgets.QMainWindow):
def __init__(self):
super(newWindow, self).__init__()
self.initnewUI()
#init new window
def initnewUI(self):
self.setWindowTitle('Editor')
self.setGeometry(300, 150, 500, 300)
self.setWindowIcon(QtGui.QIcon('editor.png'))
self.menunew()
self.editorText()
self.center()
# menu for new window
def menunew(self):
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
# add open file to toolbar
tbOpenFile = QtWidgets.QAction(QtGui.QIcon('openfile.png'), 'Open File', self)
tbOpenFile.setShortcut('Ctrl+O')
tbOpenFile.setStatusTip('Open File')
tbOpenFile.triggered.connect(self.fileOpen)
fileMenu.addAction(tbOpenFile)
# add save file to toolbar
tbSaveFile = QtWidgets.QAction(QtGui.QIcon('savefile.png'), 'Save File', self)
tbSaveFile.setShortcut('Ctrl+G')
tbSaveFile.setStatusTip('Save File')
tbSaveFile.triggered.connect(self.saveFile)
fileMenu.addAction(tbSaveFile)
# open new file
def fileOpen(self):
nombre, ok = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file')
if ok:
file = open(nombre, 'r')
self.editorText()
with file:
text = file.read()
self.textEdit.setText(text)
# save file
def saveFile(self):
name, ok = QtWidgets.QFileDialog.getSaveFileName(self, 'Save File')
if ok:
file = open(name, 'w')
text = self.textEdit.toPlainText()
file.write(text)
file.close()
# center new window
def center(self):
cent = self.frameGeometry()
center = QtWidgets.QDesktopWidget().availableGeometry().center()
cent.moveCenter(center)
self.move(cent.topLeft())
# text editor
def editorText(self):
self.textEdit = QtWidgets.QTextEdit()
self.setCentralWidget(self.textEdit)
def main():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Alguna sugerencia?
hwnd
?window.show()