4

I guess that I am not closing my PyQT5 Window correctly. I am using spyder (3.3.5) which I have installed with anaconda, to program a pyqt5 program. I am using qt creator to design my ui file, which I load using the loadUi function in pyqt package. Everything works fine with the code, until I need to close it. I close the window via the close button (the x button in the top right). The window itself is closed, but it seems like the console (or shell) is stuck, and I can't give it further commands or to re run the program, without having to restart the kernel (to completely close my IDE and re opening it).

I have tried looking for solutions to the problem in the internet, but none seems to work for me. Including changing the IPython Console > Graphics to automatic.

Edit: Also Created an issure: https://github.com/spyder-ide/spyder/issues/9991

import sys
from PyQt5 import QtWidgets,uic
from PyQt5.QtWidgets import QMainWindow
class Mic_Analysis(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui=uic.loadUi("qt_test.ui",self)
...
if __name__ == "__main__":
    def  run_app():
        if not QtWidgets.QApplication.instance():        
            app = QtWidgets.QApplication(sys.argv)
        else:
            app=QtWidgets.QApplication.instance()
        mainWin=Mic_Analysis()
        mainWin.show()
        app.exec_()
    run_app()  

If someone have any suggestion I would be very happy to hear them.

mashtock
  • 400
  • 4
  • 21
  • (*Spyder maintainer here*) Please read our [guide](https://github.com/spyder-ide/spyder/wiki/How-to-run-PyQt-applications-within-Spyder) on how to run PyQt apps inside Spyder. – Carlos Cordoba Aug 08 '19 at 12:13
  • Thank you for your response. Unfortunately I did try everything in the guide, without success... The problem persists and I still need to restart the kernel... Can it be something with the way I use loadUi? To show what I try after reading the guide, I edited the post – mashtock Aug 08 '19 at 13:48
  • Are you running this in macOS? – Carlos Cordoba Aug 08 '19 at 14:28
  • Nope. Windows 7 – mashtock Aug 08 '19 at 14:36
  • Then I think this could be related to `loadUi`. Could you create a minimal reproducible example and open an [issue](https://github.com/spyder-ide/spyder/issues) about it? Thanks! – Carlos Cordoba Aug 08 '19 at 14:42
  • 1
    Hello,I have created an issue. https://github.com/spyder-ide/spyder/issues/9991 I have also create a simple and minimal reproducible example. I really hope you can help me. First time I have opened an issue so I am not sure if I did so right, or how to follow any solution suggested there. So if you can update me here as well I would be grateful. – mashtock Aug 09 '19 at 10:43
  • @CarlosCordoba I tried also the solution of the guide but I stille have to kill Python after closing the window :/ – user1885349 Jul 19 '21 at 20:59
  • Please update to our latest version (5.0.5). This problem should be fixed on that version. – Carlos Cordoba Jul 20 '21 at 00:38
  • @CarlosCordoba I updated to spyder 5.0.5 but I still have to kill python after closing the qt5 window. Otherwise the kernel is locked. :,-/ – user1885349 Jul 21 '21 at 16:49
  • Did you activate the `Qt5` or `Automatic` backend in the IPython console preferences? – Carlos Cordoba Jul 21 '21 at 17:14
  • @CarlosCordoba none of them works. Even with the solution below. – user1885349 Jul 21 '21 at 18:21

3 Answers3

5

For me, it helped to remove the 'app.exec_()' command. But then it closes immediatly when running the code. To keep the window open, I needed to return the MainWindow instance to the global scope (or make it a global object). My code looks like this:

from PyQt5 import QtWidgets
import sys

def main():
    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance()
    main = MainWindow()
    main.show()

    return main

if __name__ == '__main__':         
    m = main()
luckycharly
  • 66
  • 1
  • 4
  • Super crazy how hard it was to track this simple solution down. – Tony A Nov 17 '20 at 14:13
  • Thank you!! I've been digging around for quite a while trying to figure this one out, special thanks for including the bit about keeping the window open. You made my day! – Chris Collett Feb 12 '21 at 20:54
  • I guys having the same issue. Every time I close the pyqt5 windows have to force the killing of Python. I dont get your solution. You completely removed the app.exec_()? – user1885349 Jul 19 '21 at 20:54
3

Try adding :

app.setQuitOnLastWindowClosed(True)

to your main() function

def main():
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(True)    
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())
Sam Gomari
  • 733
  • 4
  • 13
  • 37
0

Surprizinglly, for me this works well. No QApplication is needed.

It seems to work in another thread.

from PyQt5 import QtWidgets,uic

class Ui(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()
        uic.loadUi('./test.ui',self)
        self.show()

w=Ui()
4b0
  • 21,981
  • 30
  • 95
  • 142