Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 问题:同时使用matplotlib清除Tkinter画布小部件_Python_Matplotlib_Tkinter_Canvas - Fatal编程技术网

Python 问题:同时使用matplotlib清除Tkinter画布小部件

Python 问题:同时使用matplotlib清除Tkinter画布小部件,python,matplotlib,tkinter,canvas,Python,Matplotlib,Tkinter,Canvas,一旦在输入字段中输入了某些变量并单击了“绘图”按钮,该图将绘制散射角与探测器距离的关系。这个图显示在我创建的画布中。绘图方法是“def flup()”,大约是通过单击名为“bgra”的按钮执行的代码的一半 当我改变输入字段中的变量,然后再次点击plot时,程序会向画布添加另一个plot,这很好。我想做的是添加一个按钮,该按钮的命令是清除整个画布 from numpy import * import matplotlib.pyplot as plt fromTkinter import * imp

一旦在输入字段中输入了某些变量并单击了“绘图”按钮,该图将绘制散射角与探测器距离的关系。这个图显示在我创建的画布中。绘图方法是“def flup()”,大约是通过单击名为“bgra”的按钮执行的代码的一半

当我改变输入字段中的变量,然后再次点击plot时,程序会向画布添加另一个plot,这很好。我想做的是添加一个按钮,该按钮的命令是清除整个画布

from numpy import *
import matplotlib.pyplot as plt
fromTkinter import *
import tkMessageBox
from pylab import savefig
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg
import matplotlib.backend_bases       
from matplotlib.figure import Figure

def rutherford():
    tkMessageBox.showinfo("hello world", j)
def N():
    tkMessageBox.showinfo("Variable N", "Number of alpha particles incident on foil")

def T():
    tkMessageBox.showinfo('Variable t', 'Thickness of foil in metres')

def K():
    tkMessageBox.showinfo('Variable E', 'Kinetic energy of alpha particles in joules')

def atom():
    tkMessageBox.showinfo('Variable Z', 'Atomic number of element from which the foil is made, or the name of the element')



class App:
    def __init__(self, master):
        frame = Frame(master) #creates the first frame

        Button(frame, text = "?", command=N).grid(row=1,column=2) #various help buttons
        Button(frame, text = "?", command=T).grid(row=2,column=2)
        Button(frame, text = "?", command=K).grid(row=3,column=2)
        Button(frame, text = "?", command=atom).grid(row=4,column=2)

        Nlabel = Label(frame, text = "N =").grid(row=1, column=0, sticky=E) #labels next to the entry frame
        tlabel = Label(frame, text="t =").grid(row=2,column=0, sticky=E)    
        Elabel = Label(frame, text="E =").grid(row=3,column=0, sticky=E)        
        Zlabel = Label(frame, text="Z =").grid(row=4, column=0, sticky=E)

        eN=Entry(frame)
        eN.grid(row=1,column=1)
        et=Entry(frame)
        et.grid(row=2,column=1)
        eE=Entry(frame)
        eE.grid(row=3,column=1)
        eZ=Entry(frame)
        eZ.grid(row=4,column=1)

        def flup():         #the plot
            N=float(eN.get())#turn string into float
            t=float(et.get())
            E=float(eE.get())
            Z=float(eZ.get())
            r=10*(10**-10)
            e=1.602*(10**-19)
            a=5*(10**30)
            i = linspace(math.pi/80, math.pi, 1000)
            list =[]

            for p in i:
                b = (N*a*t)/(16.0*(r**2))
                c = ((2.0*Z*(e**2))/(E*4.0*math.pi*8.854*(10**-12)))**2
                d = (1.0/(math.sin(p/2.0)))**4
                n=b*c*d
                list.append(n)

            f=Figure(figsize=(5,4), dpi=100)
            g=f.add_subplot(1,1,1)


            g.plot(i,list)

            canvas=FigureCanvasTkAgg(f, master=master)
            canvas.show()
            canvas.get_tk_widget().pack(side=LEFT, fill=BOTH, expand=1)

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


        #def clear():


        bgra=Button(frame, text = "Plot", command=flup) #this button makes the plot
        bgra.grid(row=5,column=1)

        delete=Button(frame,text='Clear All', command=clear)
        delete.grid(row=5,column=2)




        frame2=Frame(master)

        b1=Button(frame2, text="Rutherford Scattering??", command=rutherford)
        b1.pack(side=LEFT)
        frame2.pack()
        frame.pack()



root = Tk()
app = App(root)
root.mainloop()
在plot方法“flup()”的正下方是一个名为def clear()的方法的开头,我想在其中定义一个函数,按钮将使用该函数来清除画布。你会注意到,这个按钮是在下面几行创建的,叫做“删除”。有人能给我一个示例,说明我需要在def clear()方法中包含的一小段代码,以便该按钮清除画布

from numpy import *
import matplotlib.pyplot as plt
fromTkinter import *
import tkMessageBox
from pylab import savefig
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg
import matplotlib.backend_bases       
from matplotlib.figure import Figure

def rutherford():
    tkMessageBox.showinfo("hello world", j)
def N():
    tkMessageBox.showinfo("Variable N", "Number of alpha particles incident on foil")

def T():
    tkMessageBox.showinfo('Variable t', 'Thickness of foil in metres')

def K():
    tkMessageBox.showinfo('Variable E', 'Kinetic energy of alpha particles in joules')

def atom():
    tkMessageBox.showinfo('Variable Z', 'Atomic number of element from which the foil is made, or the name of the element')



class App:
    def __init__(self, master):
        frame = Frame(master) #creates the first frame

        Button(frame, text = "?", command=N).grid(row=1,column=2) #various help buttons
        Button(frame, text = "?", command=T).grid(row=2,column=2)
        Button(frame, text = "?", command=K).grid(row=3,column=2)
        Button(frame, text = "?", command=atom).grid(row=4,column=2)

        Nlabel = Label(frame, text = "N =").grid(row=1, column=0, sticky=E) #labels next to the entry frame
        tlabel = Label(frame, text="t =").grid(row=2,column=0, sticky=E)    
        Elabel = Label(frame, text="E =").grid(row=3,column=0, sticky=E)        
        Zlabel = Label(frame, text="Z =").grid(row=4, column=0, sticky=E)

        eN=Entry(frame)
        eN.grid(row=1,column=1)
        et=Entry(frame)
        et.grid(row=2,column=1)
        eE=Entry(frame)
        eE.grid(row=3,column=1)
        eZ=Entry(frame)
        eZ.grid(row=4,column=1)

        def flup():         #the plot
            N=float(eN.get())#turn string into float
            t=float(et.get())
            E=float(eE.get())
            Z=float(eZ.get())
            r=10*(10**-10)
            e=1.602*(10**-19)
            a=5*(10**30)
            i = linspace(math.pi/80, math.pi, 1000)
            list =[]

            for p in i:
                b = (N*a*t)/(16.0*(r**2))
                c = ((2.0*Z*(e**2))/(E*4.0*math.pi*8.854*(10**-12)))**2
                d = (1.0/(math.sin(p/2.0)))**4
                n=b*c*d
                list.append(n)

            f=Figure(figsize=(5,4), dpi=100)
            g=f.add_subplot(1,1,1)


            g.plot(i,list)

            canvas=FigureCanvasTkAgg(f, master=master)
            canvas.show()
            canvas.get_tk_widget().pack(side=LEFT, fill=BOTH, expand=1)

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


        #def clear():


        bgra=Button(frame, text = "Plot", command=flup) #this button makes the plot
        bgra.grid(row=5,column=1)

        delete=Button(frame,text='Clear All', command=clear)
        delete.grid(row=5,column=2)




        frame2=Frame(master)

        b1=Button(frame2, text="Rutherford Scattering??", command=rutherford)
        b1.pack(side=LEFT)
        frame2.pack()
        frame.pack()



root = Tk()
app = App(root)
root.mainloop()

要删除画布上的所有内容,请使用参数
“all”
调用
delete
方法。我对matplotlib一无所知,但我猜您会这样做:

canvas.get_tk_widget().delete("all")

谢谢,我尝试了这一点,但奇怪的是,每次我单击“全部清除”按钮,它调用函数def clear(),包含这段代码,GUI就会在原始图形旁边打开一个新的空画布,其中仍然包含第一个绘图。我想知道这是否与读取g=f.add_子图(1,1,1)的行有关。也许程序正在打开一个新的子绘图画布,其中的内容已被删除,但保留了旧画布。不管怎样,有什么想法吗?再次感谢。相关: