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

3 投票
1 回答
1688 浏览
提问于 2025-04-17 10:50

我用qtdesigner和python写了一个小的图形界面,应该能实时显示一个粒子的轨迹在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 个回答

1

试着在for循环的最后调用一下 QCoreApplication.processEvents()

撰写回答