обновить сюжет в анимации для tkinter в python - PullRequest
1 голос
/ 17 июня 2020

У меня сложная программа, и у меня проблема с обновлением кривой в анимации, поэтому я упростил программу и опубликовал аналогичное сообщение перед предыдущим сообщением , но этот пример слишком упрощен и не может представлять актуальная проблема. Итак, этот пример исправлен, но фактическая программа по-прежнему не работает. Здесь я внес некоторые изменения и снова отправлю код.
Программа попытается обновить прямую линию дважды за время = 3 секунды и 5 секунд, проблема в том, что я не определю xlim и ylim, а затем, если обновление вне диапазона, мы не могли его увидеть; если я определю xlim и ylim, то анимация будет строить две разные кривые каждый раз, когда я использую canvas.draw (). Я также пробовал subplot1.clear () и replot, но то же самое. когда я устанавливаю blit = False, все в порядке, но я должен использовать blit = true. Похоже, анимация сохраняет в памяти два набора данных, как удалить старые данные?

from multiprocessing import Process
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
##, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import tkinter as tk
from tkinter import ttk
from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
import time
f = Figure(figsize=(6,6), dpi=100)

subplot_1 = f.add_subplot(211)
subplot_2 = f.add_subplot(212)
a_count = 0
b_count = 0
LARGE_FONT= ("Verdana", 12)
style.use("ggplot")
count_plot = 0
first_replot,second_replot = 0,0
start_time = 0
draw_replot = 0
a_t = [1,2,3,4,5]
a_T = np.ones(5)
canvas_draw_timer = 0
def replot(update_flag): 
    global a_t, count_plot,theory_line,a_T 
    if update_flag == 1:
        a_t = [3,4,5,6,7]
        a_T = np.ones(5)*2 
        theory_line[0].set_data(a_t,a_T)
    elif update_flag == 2:
        a_t = [5,6,7,8,9]
        a_T = np.ones(5)*1.5 
        theory_line[0].set_data(a_t,a_T)
    else:
        theory_line = subplot_1.plot(a_t,a_T,'c-')
    canvas.draw()

def animate(i):
    global a_count,b_count,first_replot,second_replot,canvas_draw_timer
    time1 = np.random.rand(2, 25)  

    data = np.random.rand(2, 25)*2  
    #first time update the canvas 
    if (time.time() - start_time > 3) and (first_replot == 0):
        replot(1)
        first_replot = 1
    #second time update the canvas
    if (time.time() - start_time > 5) and (second_replot == 0):
        replot(2)
        second_replot = 1
    #refresh the canvas per second, just example, many other places need to use canvas.draw() later
    if time.time() - canvas_draw_timer > 1: 
        canvas.draw()
        canvas_draw_timer = time.time()

    a = subplot_1.scatter(time1,data,c='blue',s=2)

    return a, 

class home(tk.Tk):

    def __init__(self, *args, **kwargs):        
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Graph ")       
        self.geometry("1000x1000")

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

        self.frames = {}
        F=Graph
        frame=Graph(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(Graph)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
    def get_frame(self, frame_class):
        return self.frames[frame_class]
##     
class Graph(tk.Frame):
    def __init__(self, parent, controller):
        global canvas
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text=" ", font=LARGE_FONT)
        label.pack(pady=120,padx=10)
        collectbtn=Button(self,text='collect',command=self.clickstart)
        collectbtn.place(x=200,y=100)
        canvas = FigureCanvasTkAgg(f, self)
        canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    def clickstart(self):
        global theory_line,start_time
        theory_line = subplot_1.plot(a_t,a_T,'c-')
        #set the large x and y range first, otherwise blit = true could not plot out of range plots
        f.axes[0].set_xlim([0,10])
        f.axes[0].set_ylim([0,5])
        start_time = time.time()
        aniplot_photon = animation.FuncAnimation(f, animate, blit=True,interval=100)
        canvas.draw()

app = home()
app.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...