closing a pyqt widget in ipython notebook without using sys.exit()

前端 未结 1 893
庸人自扰
庸人自扰 2021-01-15 12:42

I am trying to run through some pyqt5 tutorials in the ipython notebook, but have an issue where every second time I run a code block the kernal undergoes a forced restart.

相关标签:
1条回答
  • 2021-01-15 13:19

    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
    from PyQt5.QtWidgets import 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
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5 import QtCore
    

    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.

    0 讨论(0)
提交回复
热议问题