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_()) 
  • If it closes, it means an error in the code - Alexander

2 answers 2

Change the code to:

 self.file_vulnerabilityEdit = QTextEdit() 

Add to the top of the code, allowing any missing exception to catch, including those that occurred in Qt slots:

 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 ... if __name__ == '__main__': ... 
  • Thank you, it turned out to be very useful .. - Darkness
  • This is what the error> AttributeError is: The 'Formwidget' object has no attribute 'file_vulnerabilityEdit': ) - Darkness
  • source code added, look please, if not hard - Darkness
  • In FormWidget, you again have file_vulnerabilityEdit = QTextEdit() , but in the showDialog method you refer to it via self.file_vulnerabilityEdit . In python, you need to explicitly indicate in the constructor that the field belongs to the class using self.<название_поля> Field_name self.<название_поля> . If my answer was correct, then accept it (to the left of the answer is a check mark) and if useful, put a +1 (to the left of the answer arrow up) - gil9red
  • @Darkness, you just need to explicitly specify in python, so the reference to the object itself is explicitly passed to it in the class methods (the first argument is self ) - gil9red

Try to do the following:

 class FormWidget(QWidget): def __init__(self, parent): file_vulnerabilityEdit = QTextEdit() browse_1 = QPushButton("Display information", self) try: browse_1.clicked.connect(showDialog) except Exception as exc: print(exc) 

so you will understand where the error is.