from PyQt6.QtWidgets import QMainWindow
from PyQt6.QtCore import QThread
class Window_dev(QMainWindow):
def __init__(self):
super(Window_dev, self).__init__()
self.initUI()
def initUI(self):
self.thread()
self.worker = WorkerThread()
self.worker.start()
self.show()
class WorkerThread1(QThread):
def run(self):
try:
os.system("python server.py")
except Exception as err:
print(err)
def main():
app = QApplication(sys.argv)
ex = Window_dev()
sys.exit(app.exec())
if __name__ == '__main__':
main()
When server.py
encounters an error and crashes, the error should be captured by my python program. When I run this nothing is being caught.
sorry, this was closed by the admins because they think that there is no solution but there is a solution. here is the solution for everyone's benefit:
import sys
from PyQt5.QtCore import QProcess
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog, QTextEdit, QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
# Create a "Open File" action with a keyboard shortcut
open_file_action = QAction('Open File', self)
open_file_action.triggered.connect(self.run_file)
open_file_action.setShortcut('Ctrl+O')
# Add the action to a menu
file_menu = self.menuBar().addMenu('File')
file_menu.addAction(open_file_action)
# Create a QTextEdit widget to display error output
self.text_edit = QTextEdit(self)
self.setCentralWidget(self.text_edit)
def run_file(self):
# Open a file dialog to choose a Python file
file_name, _ = QFileDialog.getOpenFileName(self, 'Open Python File', '', 'Python Files (*.py)')
if file_name:
# Create a QProcess to run the Python program
process = QProcess(self)
process.setProgram('python')
process.setArguments([file_name])
process.readyReadStandardError.connect(self.read_error_output)
# Start the process
process.start()
def read_error_output(self):
# Read the error output from the QProcess
data = self.sender().readAllStandardError().data().decode('utf-8')
# Display the error output in the QTextEdit widget
self.text_edit.insertPlainText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
this program is very simple, it can run the selected python program and capture the standard errors like attribute or division by zero errors