Events 通过ipython/jupyter笔记本单元更新PyQt小部件
我有一个恼人的问题,过去几个月我一直没能解决。基本上,我使用jupyter/ipython笔记本调用pyqt并显示3d几何数据。这就是我将应用程序初始化为对象的方式,在添加一些多边形和点后,我调用show() 我想通过笔记本电脑单元格不断地交互/更新小部件。但是,一旦我在jupyter笔记本中调用show()命令,我就不能再运行任何单元格或更新小部件,因为笔记本输出排队(?)并被锁定:Events 通过ipython/jupyter笔记本单元更新PyQt小部件,events,pyqt,ipython,jupyter,Events,Pyqt,Ipython,Jupyter,我有一个恼人的问题,过去几个月我一直没能解决。基本上,我使用jupyter/ipython笔记本调用pyqt并显示3d几何数据。这就是我将应用程序初始化为对象的方式,在添加一些多边形和点后,我调用show() 我想通过笔记本电脑单元格不断地交互/更新小部件。但是,一旦我在jupyter笔记本中调用show()命令,我就不能再运行任何单元格或更新小部件,因为笔记本输出排队(?)并被锁定: #Initialize figure object inside the notebook fig = plb
#Initialize figure object inside the notebook
fig = plb.figure()
...
fig.show() #Locks out any further jupyter commands while widget on screen
fig.update() #Does not get executed until widget is closed
通过notebook调用的.show()函数似乎放弃了对python内核的控制(?),但不清楚如何将其取回,以及如何将其连接到正在显示的小部件
鼠标和键盘事件确实与小部件交互,但它们使用小部件代码中的内在函数,如mousemoveent():
class GLWindow(QtGui.QWidget):
def __init__(self, fig, parent=None):
QtGui.QWidget.__init__(self, parent)
self.glWidget = GLWidget(fig, parent=self)
...
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, fig, parent=None):
QtOpenGL.QGLWidget.__init__(self, parent)
...
def mouseMoveEvent(self, event):
buttons = event.buttons()
modifiers = event.modifiers()
dx = event.x() - self.lastPos.x()
dy = event.y() - self.lastPos.y()
...
我试着遵循相关建议,但我不明白如何在小部件之外使用连接或事件
感谢您的帮助,我花了这么多时间试图解决这个问题,真是尴尬。Cat我在jupyter论坛的帮助下找到了解决方案。显然,这里描述的笔记本中有一个运行时技巧,允许您动态地与glwindow交互。很高兴终于解决了这个问题 以下是整个函数,以防将来删除该示例:
#!/usr/bin/env python
"""Simple Qt4 example to manually test event loop integration.
This is meant to run tests manually in ipython as:
In [5]: %gui qt
In [6]: %run gui-qt.py
Ref: Modified from http://zetcode.com/tutorials/pyqt4/firstprograms/
"""
from PyQt4 import QtGui, QtCore
class SimpleWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 200, 80)
self.setWindowTitle('Hello World')
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect(quit, QtCore.SIGNAL('clicked()'),
self, QtCore.SLOT('close()'))
if __name__ == '__main__':
app = QtCore.QCoreApplication.instance()
if app is None:
app = QtGui.QApplication([])
sw = SimpleWindow()
sw.show()
try:
from IPython.lib.guisupport import start_event_loop_qt4
start_event_loop_qt4(app)
except ImportError:
app.exec_()
#!/usr/bin/env python
"""Simple Qt4 example to manually test event loop integration.
This is meant to run tests manually in ipython as:
In [5]: %gui qt
In [6]: %run gui-qt.py
Ref: Modified from http://zetcode.com/tutorials/pyqt4/firstprograms/
"""
from PyQt4 import QtGui, QtCore
class SimpleWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 200, 80)
self.setWindowTitle('Hello World')
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect(quit, QtCore.SIGNAL('clicked()'),
self, QtCore.SLOT('close()'))
if __name__ == '__main__':
app = QtCore.QCoreApplication.instance()
if app is None:
app = QtGui.QApplication([])
sw = SimpleWindow()
sw.show()
try:
from IPython.lib.guisupport import start_event_loop_qt4
start_event_loop_qt4(app)
except ImportError:
app.exec_()