PyQt5 gives error code after execution of example code

0

After execution of the following code

# ===

from PyQt5.QtWidgets import QApplication, QLabel

app = QApplication([])
label = QLabel('Hello World!')
label.show()
app.exec_()

# ===

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QApplication, QPushButton

app = QApplication([])
app.setStyle('Fusion')
palette = QPalette()
palette.setColor(QPalette.ButtonText, Qt.red)
app.setPalette(palette)
button = QPushButton('Hello World')
button.show()
app.exec_()

I receive the error:

Process finished with exit code -1073741819 (0xC0000005)

I am using Python 3.7, PyQt5 in PyCharm version 2019.1.3 and on Windows 10. I have reinstalled packages, reinstalled Pycharm, but to no avail.

It might be a Windows-specific bug.. or I am violating a PyQt-way-of-working by redefining the app-variable.

I can run both 'programs' individually by executing them in separate python environments, but not one after the other. If I execute the code NOT in Pycharm I see that I either get kicked out of Python after defining app.setPalette(palette), having ran the first part. Or after label = QLabel('Hello World!'), after having ran the second part.

Any extra information as to WHY this is happening would be nice :) Since I dont understand that part. Solution as to "just dont do that", won't help me in understanding the problem. Thanks in advance

python
pyqt
asked on Stack Overflow Jul 10, 2019 by zwep • edited Jul 10, 2019 by eyllanesc

1 Answer

0

Try it:

from PyQt5.QtWidgets import QApplication, QLabel, QPushButton
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtGui import QPalette


def closeEvent():
    app = QApplication([])
    app.setStyle('Fusion')
    palette = QPalette()
    palette.setColor(QPalette.ButtonText, Qt.red)
    app.setPalette(palette)
    button = QPushButton('QPushButton: Hello World')
    button.show()
    app.exec_()


app = QApplication([])

app.aboutToQuit.connect(closeEvent)                  # +++

label = QLabel('QLabel: Hello World!')
label.show()
app.exec_()

enter image description here

answered on Stack Overflow Jul 10, 2019 by S. Nick

User contributions licensed under CC BY-SA 3.0