不使用mainloop()更新tkinter / matplotlib python

时间:2019-02-27 21:02:36

标签: python matplotlib tkinter

有没有办法从另一个文件手动更新Tkinter图?

我有另一个文件需要连续轮询(为True时),所以我认为在plots类中不能有mainloop,因为它将阻塞我的主类。有什么办法可以从另一个文件中调用方法“ plot_data”并使图表(a,b,c,d)实时更新?目前,该帧冻结并且无法看到实时更新。

但是,当程序以调试模式运行时,它似乎是实时更新的。 Pycharm中的RUN / DEBUG之间的执行行为是否有所不同是有原因的吗?

下面是Plots类的视图:

class Plots(tk.Tk):
def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    tk.Tk.wm_title(self, "GUI")

    # TODO Make sure 'zoomed' state works on all laptops
    self.state('zoomed')  # Make fullscreen by default

    container = tk.Frame(self, bg='blue')
    container.pack(side="top", fill="both", expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    frame = GraphPage(container)
    self.page = GraphPage
    frame.configure(background='black')
    self.frame = frame
    frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame()  # Display frame on window

def show_frame(self):
    frame = self.frame
    frame.tkraise()

def plot_data(self):
    # TODO Add exception handling for opening the file
    file = open("../storage/data.csv", "r")  # Open data file for plotting

    t_list = ...
    a_list = ...
    v_list = ...
    a2_list = ...

    a.clear()
    a.plot(time_list, t_list)
    a.set_ylabel('T')

    b.clear()
    b.plot(time_list, a_list)
    b.set_ylabel('A')

    c.clear()
    c.plot(time_list, v_list)
    c.set_ylabel('V')

    d.clear()
    d.plot(time_list, a2_list)
    d.set_xlabel('Time(s)')
    d.set_ylabel('A')

class GraphPage(tk.Frame):
def __init__(self, parent):
    tk.Frame.__init__(self, parent)

    label = tk.Label(self, bg='red', text="GUI", font=LARGE_FONT, width=400)

    label.pack(pady=10, padx=10)

    canvas = FigureCanvasTkAgg(f, self)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

另一个文件实际上就是:

while(true):
   plots.plot_data()

0 个答案:

没有答案