Несколько живых сюжетов в tkinter windows с использованием matplotlib - PullRequest
0 голосов
/ 27 апреля 2020

Я пытаюсь получить несколько прямых графиков в tkinter windows, но работает только один график. Другой - просто график. У меня вопрос, как мне заставить оба графика работать одновременно? Моя цель - получить один основной сюжет со всеми параметрами и два других с выбранными параметрами. Я использую python 3.7.

Вот код, который отвечает за построение:

figs = [Figure(), Figure()]
axs = [f.add_subplot() for f in figs]

class ResizingCanvas(Canvas):
    def __init__(self, parent, **kwargs):
        Canvas.__init__(self, parent, **kwargs)
        self.bind("<Configure>", self.on_resize)
        self.bind("<Key>", self.key)
        self.bind("<Button-1>", self.mouse)
        self.height1 = self.winfo_reqheight() - 250
        self.height2 = self.winfo_reqheight()
        self.height3 = self.height2 - self.height1
        self.width = self.winfo_reqwidth()

        self.plot_window = Frame(self, height=self.height1, width=self.width, bg='black')
        self.plot_window.pack(side=TOP, fill=BOTH, expand=True)

        self.button_window = Frame(self, height=self.height3, width=self.width, bg='black')
        self.button_window.pack(side=BOTTOM, fill=BOTH, expand=True)

        self.path = " "

        self.Loop = False
        self.init_menu()
        self.makeFile()
        self.plot_data()

    def plot_data(self):
        canvas = FigureCanvasTkAgg(figs[0], master=self.plot_window)
        canvas.draw()
        canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)

        toolbar = NavigationToolbar2Tk(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True)

class ElectricalParamPage(Canvas):
    def __init__(self,parent,**kwargs):
        Canvas.__init__(self, parent, **kwargs)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()

        self.plot_window = Frame(self, height = self.height, width=self.width, bg='black')
        self.plot_window.pack(side=TOP, fill=BOTH, expand=True)

        self.plot_data()

# FUNKCJA TWORZACA OKNO WYKRESU W OKNIE PARAM EL.
    def plot_data(self):
        canvas = FigureCanvasTkAgg(figs[1], master=self.plot_window)
        canvas.draw()
        canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)

        toolbar = NavigationToolbar2Tk(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True)

def animate(i):
    path = file_name()

    pull_data = open(path, 'r').read()
    data_list = pull_data.split('\n') # rozdzielenie pliku na pojedyncze linijki
    Lp_list = []
    t_list = []
    min_list = []
    n_list = []
    T_list = []
    U_list = []
    I_list = []

    for line in data_list:
         if len(line)>1:
            Lp, t, minv, n, T, U, I = line.split() # przypisanie po kolei wartosci z kazdej linijki
            Lp_list.append(int(Lp))
            t_list.append(int(t)/1000) # czas w sekundach
            min_list.append(int(minv))
            n_list.append(int(n))
            T_list.append(int(T))
            U_list.append(int(U))
            I_list.append(int(I))

    axs[0].clear()

    axs[0].set_ylabel('Prędkość [obr/min]')
    axs[0].set_xlabel('Czas [s]')

    axs[0].plot(t_list, n_list, label='Prędkość obrotowa')
    axs[0].plot(t_list, T_list, label='Moment obrotowy')
    axs[0].plot(t_list, Lp_list, label='Liczba pojedyncza')

    axs[0].xaxis.set_major_locator(plt.MultipleLocator(5))
    axs[0].yaxis.set_major_locator(plt.LinearLocator(numticks=6))

    axs[0].legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=3, borderaxespad=0)

def animate2(i):
    path = file_name()

    pull_data = open(path, 'r').read()
    data_list = pull_data.split('\n')  # rozdzielenie pliku na pojedyncze linijki

    t_list = []
    U_list = []
    I_list = []
    Lp_list = []

    for line in data_list:
        if len(line)>1:
            Lp, t, minv, n, T, U, I = line.split() # przypisanie po kolei wartosci z kazdej linijki
            Lp_list.append(Lp)
            t_list.append(int(t)/1000) # czas w sekundach
            U_list.append(int(U))
            I_list.append(int(I))

    axs[1].clear()

    axs[1].set_ylabel('Napięcie [V]')
    axs[1].set_xlabel('Czas [s]')

    axs[1].plot(t_list, U_list, label='Napięcie')
    axs[1].plot(t_list, I_list, label='Prąd')
    axs[1].plot(t_list, Lp_list, label='Liczba pojedyncza')

    axs[1].xaxis.set_major_locator(plt.MultipleLocator(5))
    axs[1].yaxis.set_major_locator(plt.LinearLocator(numticks=6))

    axs[1].legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=3, borderaxespad=0)

def main():
    root = Tk()
    root.grid_rowconfigure(10, weight=1)
    root.grid_columnconfigure(2, weight=1)

    root.title("Projekt zespołowy")
    # root.resizable(width=FALSE, height=FALSE)
    root.geometry('{}x{}'.format(1320, 850))

    mainframe = Frame(root, bg='black', width=1320, height=850)
    mainframe.pack(fill=BOTH, expand=YES)
    win = ResizingCanvas(mainframe, width=1320, height=850, bg="black", highlightthickness=0)
    win.pack(fill=BOTH, expand=YES)

    #anis = []
    #funcs = [animate, animate2]

    ani = animation.FuncAnimation(figs[0], animate, interval=100)
    ani2 = animation.FuncAnimation(figs[1], animate2, interval=100)

    root.mainloop()

if __name__ == "__main__":
    main()

Вот главная страница с графиком, которая работает правильно: enter image description here

Вот вторая страница, которая не работает: enter image description here

...