Pyqt Terminal hangs after excuting close window command
<p>I have read lots of threads online, but still I could not find the solution. My question should be very simple: how to close a Pyqt window WITHOUT clicking a button or using a timer.
The code I tried is pasted below</p>

<pre><code>from PyQt4 import QtGui, QtCore
import numpy as np
import progressMeter_simple
import sys
import time
import pdb
class ProgressMeter(progressMeter_simple.Ui_Dialog, QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        progressMeter_simple.Ui_Dialog.__init__(self)
        self.setupUi(self)
        self.progressBar.setRange(0, 0)
        QtGui.QApplication.processEvents()
    def termination(self):
        time.sleep(5)
        self.close()
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    Dialog = ProgressMeter()
    Dialog.show()
    Dialog.termination()
    sys.exit(app.exec_())
</code></pre>

<p>My Pyqt GUI is designed using Qt designer, and it is nothing but a progress bar that keeps moving from left to right (busy indication).</p>

<p>However, when I run the code above, the terminal still hangs after the Pyqt window is closed. Ctrl+C also couldn't kill the process. 
In short, how can I properly close/terminate a Pyqt window without clicking a button or using a timer?</p>

<p>It's not working because you're calling GUI methods on the dialog (<code>close()</code>) outside of the event loop.  The event loop doesn't start until you call <code>app.exec_()</code>.</p>

<p>If you really want to close the dialog <em>immediately</em> after it opens without using a <code>QTimer</code>, you can override the <a href="http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#showEvent" rel="nofollow"><code>showEvent()</code></a> method and call <code>termination()</code> from there, which gets called when the dialog is first displayed.</p>

<pre><code>class ProgressMeter(progressMeter_simple.Ui_Dialog, QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        progressMeter_simple.Ui_Dialog.__init__(self)
        self.setupUi(self)
        self.progressBar.setRange(0, 0)

    def showEvent(self, event):
        super(ProgressMeter, self).showEvent(event)
        self.termination()
</code></pre>

python pyqt pyqt4