код не исполняется после app.exec_()

Как сделать так, чтобы код после app.exec_() был выполнен?

from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

app = QApplication([])
window = MainWindow()

text = QLabel(window)

window.show()
app.exec_()

text.setText("Hello, world")      # этот код не будет выполнен :(

Ответы (1 шт):

Автор решения: S. Nick

Так как вы задумали - не получится. Попробуйте так:

from PyQt5.QtWidgets import QMainWindow, QLabel, QApplication
from PyQt5.QtCore import QThread

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.label = QLabel("Label", self)

    def closeEvent(self, event):
        self.label.setText("Hello, world")
        QApplication.processEvents()        
        QThread.msleep(3000)   
        
        
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    try:
        sys.exit(app.exec_())
    except SystemExit:
        print('Closing Window... ')
→ Ссылка