Python:PyQtGraph没有显示动画

时间:2018-01-02 21:09:52

标签: python graph pyqtgraph

我有一个包含多个节点的图表。一旦用户单击任何节点,图形就应该以这样的方式进行双曲线处理:单击的节点应该位于图形的中心(通常是2D坐标系原点的位置)。当我点击图表中没有更新时,节点的新坐标值(散点)会在本地保存的文件中更新。下面是用于渲染和更新的函数。

# Function to render the H2 tree map embedded in Poincare disc
def render_h2_tree(positions):

    global adj
    global nodeText
    # Configuring the PyQt graphic window
    pg.setConfigOption('background', 'k')
    pg.setConfigOption('foreground', 'w')
    pg.setConfigOptions(antialias=True)

    w = pg.GraphicsWindow()                 # creating an instance of the PyQt GraphicsWindow
    w.setWindowTitle('H2 tree for Emails')  # set the title of the graphic window
    v = w.addViewBox()                      # add a view box to the graphic window
    v.setAspectLocked()
    g = Graph()                             # create an instance of the class Graph
    v.addItem(g)                            # add the created graph instance to the view box

    g.setData(pos=positions, adj=adj, size=0.01, pxMode=False, text=nodeText)  # set the node in the graphic window

    # Start Qt event loop unless running in interactive mode or using pyside.
    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

我在这里做错了什么?谢谢。

1 个答案:

答案 0 :(得分:0)

所以我能够解决这个问题。修改后代码如下:

w = pg.GraphicsWindow()                 # creating an instance of the PyQt GraphicsWindow
v = w.addViewBox()                      # add a view box to the graphic window
g = Graph()                             # create an instance of the class Graph

# Function to render the H2 tree map embedded in Poincare disc
def render_h2_tree(positions):

    global adj
    global nodeText
    global w
    global v
    global g

    # Configuring the PyQt graphic window
    pg.setConfigOption('background', 'k')
    pg.setConfigOption('foreground', 'w')
    pg.setConfigOptions(antialias=True)

    w.setWindowTitle('H2 tree for Emails')  # set the title of the graphic window        
    v.setAspectLocked()        
    v.addItem(g)                            # add the created graph instance to the view box

    g.setData(pos=positions, adj=adj, size=0.01, pxMode=False, text=nodeText)  # set the node in the graphic window

    # Start Qt event loop unless running in interactive mode or using pyside.
    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

在将图形窗口,视图框和图形声明为全局项目之后,我确保不为它创建任何新实例。这样,当单击窗口中的任何节点时,所有这些项的实例保持不变,然后可以将节点绘制到新的2D位置。