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


i trying run through pyqt5 tutorials in ipython notebook, have issue every second time run code block kernal undergoes forced restart. here smallest code causes problem:

import sys pyqt5.qtwidgets import qapplication, qwidget  if __name__ == '__main__':      app = qapplication(sys.argv)      w = qwidget()     w.setwindowtitle('simple')     w.show()      sys.exit(app.exec_()) 

i running ipython3 notebook , python 3, pyqt5, , using ubuntu 14.04. should noted problem not occur when running same code via script in terminal.

some other unrelated questions have suggested problem due sys.exit() messing instance of python(i hope correct term) instead of closing pyqt application. happens first time run code, second time runs kernel forced restart. problem? , if how work around this?

if more info required, please ask.

i tried out taar's solution still got dead kernel after calling cell main more twice.the problem creating multiple qapplications, crashes notebook.

there multiple solutions found, being able run qt application use following in first cell:

%gui qt pyqt5.qtwidgets import qapplication, qwidget 

and in second cell:

 if __name__ == '__main__':          w = qwidget()         w.setwindowtitle('simple')         w.show() 

you can call second cell many times want , work. magic line %gui qt opens qapplication notebook.

if need more control (like being able exit() it) there various solutions amount checking if there qapplication instance open. here example:

import sys pyqt5.qtwidgets import qapplication, qwidget pyqt5 import qtcore 

second cell:

if __name__ == '__main__':      app = qtcore.qcoreapplication.instance()     if app none:         app = qapplication(sys.argv)      w = qwidget()     w.setwindowtitle('simple')     w.show()      app.exec_() 

this method require closing window before rerunning (else queued up: run 3x without closing window, need close window 3x in row). @ least started loaded screen upon executing cell. (anyone feel welcome correct example).

some references second example: here, , here. don't know enough how qt gui interacts notebook solve problem above example.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -