简体   繁体   中英

How to update a matplotlib figure, from a function, in a Qt GUI

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM