如何使用Tkinter可视化时间序列数据?

时间:2017-05-18 05:50:29

标签: python tkinter

我正在尝试使用Tkinter来显示一些时间序列数据。我的数据采用2D矩阵的形式,行指的是指一段特定时间段的块和列。所有值都在0到1之间。

我想用Tkinter创建一个python脚本,创建一个窗口,用方形矩阵显示块,第一列确定每个块的亮度,然后在预定的时间后,根据改变块的亮度到数据中的连续列。

我创建了迄今为止我所拥有的缩减版本:

ctypes.POINTER

这似乎做了我想要的,但它每次都直接跳到最后一帧 - 即。它不会绘制一帧,暂停,绘制另一帧,再次暂停等等。

任何人都可以帮我解决一下我做错了吗?

1 个答案:

答案 0 :(得分:1)

您的问题是,当您致电:

for t in xrange(nT):
    root.after(1000, redrawPeriod,t)

root.after()不会阻止执行,因此for循环执行速度非常快,并且所有重绘事件都会在1000 ms后同时调用。

在Tkinter中运行动画的常用方法是编写一个动画方法,该方法在延迟后调用自身(有关详细信息,请参阅Method for having animated movement for canvas objects pythonTkinter, executing functions over time)。

在你的情况下,你可以这样做:

# Create all widgets
...

# Store the period indices to be redrawn ([0, 1, 2, ..., nT-1])
periods = range(nT)

# Define the animation method
def animation_loop(delay):
    # Check if there are still periods to be drawn
    if len(periods) > 0:
        t = periods[0]
        redrawPeriod(t)
        # Remove the period that was just drawn from the list of periods
        del periods[0]
        # Call the animation_loop again after a delay
        root.after(delay, animation_loop, delay)

# Start the animation loop
animation_loop(1000)

# Launch the app
root.mainloop()
相关问题