0

I have been experimenting with PyQt5 and testing very basic examples I've found online. I'm running this using anaconda spyder and python 3.7. The first time I run the code, the window appears fine and how I'd expect it to look. But the second time I try to run it, the window doesn't appear at all, it looks like my IPython console restarts and I have to manually terminate the code to get it to respond. I suspected it was because I wasn't terminating properly in the code but I've tried multiple ways to terminate including sys.exit(), exit, quit, making a class and app=QApplication(sys.argv) followed by sys.exit(app.exec_()), etc. I still get the same issue. What could it be

Here's some examples I tried

Ex:

import sys
from PyQt5.QtWidgets import QPushButton, QApplication, QGridLayout, QVBoxLayout, QWidget, QGroupBox

app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
layout.addWidget(QPushButton('Top'))
layout.addWidget(QPushButton('Bottom'))
window.setLayout(layout)
window.show()
app.exec_()

Another Example:

app = QApplication([])
window=QWidget()
layout=QGridLayout()
groupbox=QGroupBox("Box")
vbox=QVBoxLayout()
vbox.addWidget(QPushButton("Button"))
groupbox.setLayout(vbox)
layout.addWidget(groupbox, 0, 0)
window.setLayout(layout)
window.show()
app.exec_()
Diego
  • 216
  • 1
  • 11
Gingerhaze
  • 664
  • 2
  • 5
  • 13
  • So I did this and now after I run it once it never terminates. Plus after I manually kill it, it freezes and I have to restart the kernel – Gingerhaze Oct 16 '19 at 17:22
  • @Gingerhaze If you run these scripts from a normal console (i.e. not in an IDE), they should work correctly. However, you may need to do things differently when running them from an IPython console. The problem is that it may result in multiple `QApplication` objects being created, which is not supported by Qt. – ekhumoro Oct 16 '19 at 19:02

0 Answers0