3

I am trying to create a simple GUI using PyQt5. I am running my code in windows 10 from Spyder(Anaconda latest version, python 3.7). Here is my code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button test'
        self.left = 50
        self.top = 50
        self.width = 720
        self.height = 800
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100,70)
        button.clicked.connect(self.on_click)

        self.show()

    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    app.exec_()

A window pop up. Now if I close the GUI by clicking on closing button(top right corner of the GUI),Spyder IP console does not return to normal condition. It freezes. What should I use in code to solve the issue?

Thanks, Moni

Moni
  • 31
  • 2
  • 3
  • (*Spyder maintainer here*) Please read [our guide](https://github.com/spyder-ide/spyder/wiki/How-to-run-PyQt-applications-within-Spyder) on how to develop PyQt apps in Spyder. – Carlos Cordoba Jan 05 '19 at 01:15
  • @Carlos, I ran the example code from your link but it did not solve my issue. If I click on the "X" button on the top right corner, Spyder console freezes. – Moni Jan 07 '19 at 19:28
  • 2
    Ok, to avoid that, please go to `Tools > Preferences > IPython console > Graphics` turn off the option called `Activate support` and restart Spyder. – Carlos Cordoba Jan 07 '19 at 20:46

2 Answers2

4

Closing the window doesn't appear to close the QApplication object. To do this, override the closeEvent function of the main window by adding these two lines after the on_Click definition

  def closeEvent(self,event):
     QApplication.quit()

This will close the window and the QApplication object as well and control should return to the console

roomend
  • 71
  • 5
0

You can try and look at the answer in the following thread: Can't Kill PyQT Window after closing it. Which requires me to restart the kernal

Seems to have resemblance to your problem and a solution.

mashtock
  • 400
  • 4
  • 21