如何使用TkAggBackend编写关闭matplotlib图的方法

时间:2018-02-20 03:59:24

标签: python-3.x matplotlib tkinter tkinter-canvas

我将matplotlib图形嵌入到tkinter中。我已经尝试过编写一个方法来使用plt.clf(图),get_tk_widget()。destroy()等等...在我的搜索中,我没有找到任何关于嵌入式方法/函数的文档示例来自后端的图表。

我显然在这里遗漏了一些东西,但我真的很新。我根本无法找到一种方法来编写一个方法,以便我可以调用get_tk_widget()。destroy()。我不确定如何从关闭它的方法中引用我的小部件。我认为这可能是一个简单的参考错误,基于我在 init 中定义的方式,但我不太了解tkinter以确定无疑。

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib import style
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename as getname

import numpy as np

from datetime import datetime


Large_Font = ("Arial", 12)

style.use("ggplot")

class ratgraph(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "RA Trend Grapher")

        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 = {}

        for F in (MainPage, PageOne, PageTwo):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MainPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class MainPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Main Page", font=Large_Font)
        label.pack(padx=10, pady=10)

        button1 = ttk.Button(self, text="Open Trend File", command=lambda: self.RetrFileName())
        button1.pack()

        button2 = ttk.Button(self, text="Close Graph", command=lambda: self.CloseGraph())
        button2.pack()

    def RetrFileName(self):
        fname = getname(initialdir="C:/Users/Zibit/Desktop/",
                        filetypes=(("RA CSV File", "*.csv"), ("All Files", "*.*")),
                        title="Load a Trend")

        fname = fname.replace("\\", "/")

        data = np.genfromtxt(fname, delimiter=",", dtype=(str, str, "U40", float, float, float),
                             names=["a", "b", "c", "d", "e", "f"])

        x = [datetime.strptime(i, "%H:%M:%S;%f") for i in data["c"]]
        y = data["d"]

        ToDateFmt = mdates.DateFormatter("%M:%S:%f")

        f = Figure(figsize=(8, 5), dpi=96)
        a = f.add_subplot(111)
        a.xaxis.set_major_formatter(ToDateFmt)
        a.plot(x, y, label="Carriage Current Output")
        plt.setp(a.get_xticklabels(), rotation=15)
        '''

        plt.xlabel("Time")
        plt.ylabel("Amps")
        plt.title("RA Trend Grapher")
        plt.legend()

        '''

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

        toolbar = NavigationToolbar2TkAgg(canvas, self)
        toolbar.update()

        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    def CloseGraph(self):
        ??? get_tk_widget().destroy() ???

任何帮助或示例链接都将受到赞赏。

0 个答案:

没有答案
相关问题