#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Clearing mt tkinter canvas


    Hi everyone. I have made a GUI using tkinter. The GUI is a window containing four entry field widgets, seven buttons and some labels.

    The user inputs values into the entry field widgets then hits plot and a canvas opens and in it appears a plot. plotted by the method 'def flup()', (which starts about halfway down the code). Calling this method also adds another button labelled 'clear all' to the GUI. This button calls the method 'def clear()' which clears the canvas which is currently showing the plot.

    Everything up until this point is fine. If I re-enter data and hit plot again a second canvas opens (or at least another plot within the same canvas, I'm not sure which) displaying this updated plot, to the right of the previous. I could live with this if my 'clear all' button would clear everything so that i can start again but it only clears the plot I've just done.
    For example if I entered some values then hit 'plot', the canvas would open and a plot would appear. I could then click 'clear all' and the plot would vanish leaving the canvas open but empty. Then if I re-enter data and click 'plot' the program does not add the plot to the same space where it just was, but to the right of that. If I then click 'clear all' that plot vanishes and I am left with an empty canvas which is the size of two of these plots.


    I have been staring at this for days and am having serious code blindness. Any help would be greatly appreciated. Here is my code (sorry, there's lots of it)...


    Code:
    from numpy import *
    from scipy.optimize import curve_fit
    import matplotlib.pyplot as plt
    from Tkinter import *
    import tkMessageBox
    from pylab import savefig
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    import matplotlib.backend_bases       
    from matplotlib.figure import Figure
    c = open('napalm.dat','r')
    j=c.read()
    
    def rutherford():
    	tkMessageBox.showinfo("Rutherford Scattering", 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')
    	
    def exp():
    	import matplotlib.pyplot as plt
    	k=genfromtxt('experiment.dat')
    	print k
    	plt.plot(k)
    	plt.show()
    	
    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)
    		Button(frame, text='Plot from experimental data',command=exp).grid(row=5,column=5)
    				
    		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=int(eN.get())		#turn string into integer
    			t=float(et.get())		#turn string into float
    			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(111)
    			g.plot(i,list)
    			
    			
    			canvas=FigureCanvasTkAgg(f, master=master)     #I think the problem is somewhere around here
    			h=canvas.get_tk_widget()				
    			h.pack(side=LEFT, fill=BOTH, expand=1)
    			
    			
    		
    			def clear():
    				canvas.get_tk_widget().delete("all")
    			
    			
    			
    			delete=Button(frame,text='Clear All', command=clear)
    			delete.grid(row=5,column=2)
    			
    			#update=Button(frame, text = "Update", command=flup) #this button makes the plot
    			#update.grid(row=5,column=3)
    		
    		bgra=Button(frame, text = "Plot", command=flup) #this button makes the plot
    		bgra.grid(row=5,column=1)
    		
    		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()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    This may be a little closer to what you need.
    Code:
    		
    			def clear():
    				canvas.get_tk_widget().destroy()
    				#canvas.get_tk_widget().delete("all")
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Thanks for the response. This certainly is closer to what I had in mind. There are still some issues with this. Now I click plot and the canvas opens and displays the plot, then I must call the method you suggested before plotting again which is fine. If I plot twice in a row without destroying then I get two plots side by side then calling clear() just destroys the last plot made, not both.

    However for my purposes I think this will do, as after every plot I will destroy it before re-plotting.

    Thanks a million buddy.

IMN logo majestic logo threadwatch logo seochat tools logo