Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 12th, 2012, 08:40 AM
zwap091 zwap091 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 zwap091 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 49 sec
Reputation 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()

Reply With Quote
  #2  
Old December 12th, 2012, 11:19 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,390 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 35 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old December 12th, 2012, 08:16 PM
zwap091 zwap091 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 zwap091 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 49 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Clearing mt tkinter canvas

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap