如何在Qt GUI中从函数更新matplotlib图

时间:2012-01-22 17:35:32

标签: python matplotlib qt-designer

我用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

我的问题是,只有在函数" run()"尽管打电话给" draw()"在循环内。因此,我只有最终的轨迹,而不是完整的电影......

有没有人知道如何在此函数/循环中强制执行图形更新?

感谢。

1 个答案:

答案 0 :(得分:1)

尝试在for循环结束时调用QCoreApplication.processEvents()