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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
zwep
  • 1,207
  • 12
  • 26
  • Okay running Python 3.7 with pyqt5 on Win10 and I copy/pasted what you supplied and I get no error at all -- get a really ugly little window but it functions okay -- oh and let me clarify I pasted them into a single file and ran that file – Dennis Jensen Jul 10 '19 at 18:02
  • So I re-read your problem -- and I ran them both separately -- and I get no error with either one. Now these are 2 separate executable mini-programs so I see no reason you would be running them in a manner that they would step on one another and/or give an error at all. – Dennis Jensen Jul 10 '19 at 18:07

1 Answers1

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

S. Nick
  • 12,879
  • 8
  • 25
  • 33