如何在 Qt GUI 中从函数更新 matplotlib 图
我用 qtdesigner 和 python 编写了一个小型 GUI,它应该在 matplotlib 图中实时显示一个粒子的轨迹。所以我有这样的问题:
class DesignerMainWindow(QtGui.QMainWindow, Ui_MplMainWindow):
"""Customization for Qt Designer created window"""
def __init__(self, parent = None):
# initialization of the superclass
super(DesignerMainWindow, self).__init__(parent)
# setup the GUI --> function generated by pyuic4
self.setupUi(self)
self.niter = 30
#... other initializations
def run(self):
# set xo, yo with initial particle position
for t in range(self.niter):
# set new particle position in x, y
self.mpl.canvas.ax.plot([xo, x], [yo, y], '-b')
self.mpl.canvas.draw()
print x, y, t, self.niter
xo = x
yo = y
我的问题是,尽管在循环内调用了“draw()”,但仅当函数“run()”完成时才会更新图形。因此,我只有最终的轨迹,而不是完整的电影...
有人知道如何从这个函数/循环中强制图形更新吗?
谢谢。
I have written a small GUI with qtdesigner and python which should display in real-time the trajectory of one particle in a matplotlib figure. So I have something like:
class DesignerMainWindow(QtGui.QMainWindow, Ui_MplMainWindow):
"""Customization for Qt Designer created window"""
def __init__(self, parent = None):
# initialization of the superclass
super(DesignerMainWindow, self).__init__(parent)
# setup the GUI --> function generated by pyuic4
self.setupUi(self)
self.niter = 30
#... other initializations
def run(self):
# set xo, yo with initial particle position
for t in range(self.niter):
# set new particle position in x, y
self.mpl.canvas.ax.plot([xo, x], [yo, y], '-b')
self.mpl.canvas.draw()
print x, y, t, self.niter
xo = x
yo = y
My problem is that the figure is updated only when the function "run()" is finished, despite the call to "draw()" inside the loop. Thus I have only the final trajectory and not the full movie...
Does anyone has an idea on how to force the graph update from within this function/loop ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试调用
QCoreApplication.processEvents()
在for
循环的末尾。Try to call
QCoreApplication.processEvents()
at the end offor
loop.