Closing A Pyqt Widget In Ipython Notebook Without Using Sys.exit()
Solution 1:
I tried out Taar's solution but still got a dead kernel after calling the cell with main more than twice.The problem is creating multiple Qapplications, this crashes the notebook.
There is multiple solutions I found, but to just being able to run a qt application use the following in the first cell:
%gui qt
fromPyQt5.QtWidgetsimport QApplication, QWidget
and in the second cell:
if__name__== '__main__':
w = QWidget()
w.setWindowTitle('Simple')
w.show()
You can call the second cell as many times as you want and it will work. the magic line %gui qt
opens a QApplication for your notebook.
If you need more control (like being able to exit() it) there is various solutions that amount to checking if there is a Qapplication instance open. Here is an example:
import sys
fromPyQt5.QtWidgetsimport QApplication, QWidget
fromPyQt5importQtCore
second cell:
if __name__ == '__main__':
app = QtCore.QCoreApplication.instance()
if app is None:
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle('Simple')
w.show()
app.exec_()
This method does require closing the window before rerunning it (else they will be queued up: run 3x without closing the window, now you need to close the window 3x in a row). It will at least get you started with a properly loaded screen upon executing the cell. (anyone feel welcome to correct this example).
Some references for the second example: here, and here. But I don't know enough about how the qt gui interacts with the notebook to solve any problem with the above example.
Post a Comment for "Closing A Pyqt Widget In Ipython Notebook Without Using Sys.exit()"