替换Tkinter Python上的数字

时间:2017-06-27 15:02:58

标签: python-2.7 canvas tkinter subplot

我正在使用Tkinter,并希望能够清除我的图表并在其位置绘制一个新图表。我不知道在回调中要放什么来做这件事。到目前为止,我能做的最好的事情是用白框替换图形。新图形在下面绘制(这不是理想的),并且出于某种原因也显示为空白框。对于提出这样一个基本问题的道歉,但我是python的新手,并且无法在这里找到使用子图的示例。

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import Tkinter as tk
from Tkinter import *
import ttk



class Helmholtz_App(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Helmholtz Coils Data")

        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 (PageOne,PageTwo):

            frame = F(container, self)

            self.frames[F] = frame

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

        self.show_frame(PageOne)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()




class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        fig1 = Figure(figsize=(5,5), dpi=100)
        a = fig1.add_subplot(111)


        def callback():

            volt=[[0,1,2,3,4],[0,1,2,3,4]]
            a.plot(volt[1],volt[0],'bo')

            canvas = FigureCanvasTkAgg(fig1, 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 ClearCallback():

                #problem line
                #~~~~~~~~~~~
                fig1.clf()
                # ~~~~~~~~~~

                clearbutton.destroy()

            clearbutton=ttk.Button(self, text="Clear", command=ClearCallback)
            clearbutton.pack()

        b=ttk.Button(self, text="Plot Data", command=callback)  
        b.pack()
class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

app = Helmholtz_App()
app.mainloop()

代码基于本教程1 我希望代码能够正常运行,以便"采取数据"按钮创建一个图形,"清除"按钮删除该图表。然后当"取数据"再次按下按钮会将新图形放在同一个地方,而旧图表中没有任何数据。

这是

0 个答案:

没有答案