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

发布于 2024-12-28 11:40:37 字数 886 浏览 0 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2025-01-04 11:40:37

尝试调用 QCoreApplication.processEvents()for 循环的末尾。

Try to call QCoreApplication.processEvents() at the end of for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文