I am trying to assign a button the function of opening a file and reading in QTextEdit, but when the button is pressed, the graphical shell closes.
def log_uncaught_exceptions(ex_cls, ex, tb): import traceback text = '{}: {}:\n'.format(ex_cls.__name__, ex) text += ''.join(traceback.format_tb(tb)) QMessageBox.critical(None, 'Error', text) quit() import sys sys.excepthook = log_uncaught_exceptions from PyQt5.QtWidgets import (QMainWindow, QApplication, QLineEdit, QDesktopWidget, QMessageBox, QToolTip, QPushButton, QAction, QLabel, QGridLayout, QWidget, QTextEdit, QFileDialog) from PyQt5.QtGui import QIcon, QFont from graph.run import run class MyMainWindow(QMainWindow): def __init__(self, parent=None): super(MyMainWindow, self).__init__(parent) self.form_widget = FormWidget(self) self.setCentralWidget(self.form_widget) exitAction = QAction(QIcon('D:/Programming/Python/mo-mo/GUI/icon/exit.png'), '&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit') exitAction.triggered.connect(self.closeEvent) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAction) exitAction = QAction(QIcon('D:/Programming/Python/mo-mo/GUI/icon/exit.png'), 'Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.triggered.connect(self.closeEvent) self.toolbar = self.addToolBar('Exit') self.toolbar.addAction(exitAction) self.resize(500, 300) self.center() self.setWindowTitle('Spider') self.setWindowIcon(QIcon('D:/Programming/Python/mo-mo/GUI/icon/1.png')) self.show() def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def closeEvent(self, event): reply = QMessageBox.question(self, 'Exit', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore() class FormWidget(QWidget): def __init__(self, parent): super(FormWidget, self).__init__(parent) QToolTip.setFont(QFont('SansSerif', 10)) name = QLabel('Selected files:') self.file_vulnerability = QLabel('File vulnerability:') link_file = QLabel('Link file:') network_topology = QLabel('Network topology:') start_ip = QLabel('Start IP:') file_vulnerabilityEdit = QTextEdit() link_fileEdit = QTextEdit() network_topologyEdit = QTextEdit() start_ipEdit = QLineEdit() browse_1 = QPushButton("Display information", self) browse_2 = QPushButton("Display information", self) browse_3 = QPushButton("Display information", self) search = QPushButton("Search", self) grid = QGridLayout() grid.setSpacing(5) grid.addWidget(name, 0, 0) grid.addWidget(self.file_vulnerability, 1, 0) grid.addWidget(file_vulnerabilityEdit, 2, 0) grid.addWidget(browse_1, 3, 0) grid.addWidget(link_file, 1, 1) grid.addWidget(link_fileEdit, 2, 1) grid.addWidget(browse_2, 3, 1) grid.addWidget(network_topology, 1, 2) grid.addWidget(network_topologyEdit, 2, 2) grid.addWidget(browse_3, 3, 2) grid.addWidget(start_ip, 4, 0) grid.addWidget(start_ipEdit, 4, 1) grid.addWidget(search, 4, 2) self.setLayout(grid) self.show() search.clicked.connect(run) try: browse_1.clicked.connect(self.showDialog) except Exception as exc: print(exc) def showDialog(self): fname = QFileDialog.getOpenFileName(self, 'Open file', 'D:/Programming/Python/mo-mo/GUI')[0] f = open(fname, 'r') with f: data = f.read() self.file_vulnerabilityEdit.setText(data) if __name__ == '__main__': app = QApplication(sys.argv) foo = MyMainWindow() foo.show() sys.exit(app.exec_())