【发布时间】:2016-10-04 03:57:17
【问题描述】:
我有一个 Python 程序 (main.py),它有一个由 RTStreamer 类处理的恒定数据流,它基本上通过实时流获取数据并附加它到一个 numpy 数组。但是,我想实际可视化数据,因为它是 tkinter 和 matplotlib 进来的地方。在另一个 python 文件(gui.py)中,我有一个使用 matplotlib 动画和一个按钮的实时图触发我的第一个文件 (main.py) 开始流式传输数据。但是,当我单击按钮开始流式传输数据时,我可以在控制台上看到它正在获取数据并将其附加到数组中(因为我正在打印数组),但是图表根本没有更新。
这是我的 main.py 的简化版本:
closeBidArray = np.array([])
class RTStreamer(stream.Streamer):
def __init__(self, *args, **kwargs):
super(RTStreamer, self).__init__(*args, **kwargs)
print(datetime.now(), "initialized")
def on_success(self, data):
# get data and append it to closeBidArray
def on_error(self, data):
# disconnect
def run():
stream = RTStreamer()
stream.rates()
这是我的 gui.py 的样子:
import main # in order to get closeBidArray
figure = Figure(figsize=(5,5), dpi=100)
subplot1 = figure.add_subplot(111)
class App(tk.Tk):
# Mostly to do with formatting the window and frames in it
def animate():
subplot1.clear
subplot1.plot(main.closeBidArray)
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# THIS IS THE BUTTON THAT TRIGGERS TO FUNCTION THAT STREAMS THE DATA:
button1 = tk.Button(text="Hi", command=main.run)
button1.pack()
canvas = FigureCanvasTkAgg(figure, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
app = App()
ani = animation.FuncAnimation(figure, animate, interval=1000)
app.mainloop()
我注意到如果我点击 CTRL+C 来破坏程序,它会遍历流数据并绘制数组,如果我点击 CTRL+C 再次完全关闭 matplotlib 窗口。但是我想在绘制它的同时流式传输并附加到数组,关于如何实现这一点的任何想法?谢谢。
【问题讨论】:
标签: python python-3.x matplotlib tkinter