#!/usr/bin/env python
import sys
import matplotlib
matplotlib.use('GTK')

from matplotlib.figure import Figure
from matplotlib.axes import Subplot
from matplotlib.backends.backend_gtk import FigureCanvasGTK, NavigationToolbar

from matplotlib.numerix import arange, sin, pi

try:
	import pygtk
	pygtk.require("2.0")
except:
	pass
try:
	import gtk
	import gtk.glade
except:
	sys.exit(1)

class appGui:
	def __init__(self):
		gladefile = "project1.glade"
		self.windowname = "window1"
		self.wTree = gtk.glade.XML(gladefile, self.windowname)
		dic = {"on_window1_destroy" : gtk.main_quit,
			"on_button1_clicked" : self.createProjectGraph
			}
		
		self.wTree.signal_autoconnect(dic)
		# setup matplotlib stuff on first notebook page (empty graph)
		self.figure = Figure(figsize=(6,4), dpi=72)
		self.axis = self.figure.add_subplot(111)
		
		self.canvas = FigureCanvasGTK(self.figure) # a gtk.DrawingArea
		self.canvas.show()
		self.grahview = self.wTree.get_widget("vbox1")
		self.grahview.pack_start(self.canvas, gtk.TRUE, gtk.TRUE)
		
		return
	
	# callbacks.
	def createProjectGraph(self, widget):
		# empty axis if neccesary, and reset title and stuff
		self.axis.clear()
		# get data
		age = self.wTree.get_widget("entry1").get_text()
		size = self.wTree.get_widget("entry2").get_text()
		N = 1
		ind = arange(N)  # the x locations for the groups
		width = 0.35       # the width of the bars
		
		p1 = self.axis.bar(ind, int(age), width, color='r')
		p2 = self.axis.bar(ind+width, int(size), width, color='y')
		
		self.canvas.destroy()
		self.canvas = FigureCanvasGTK(self.figure) # a gtk.DrawingArea
		self.canvas.show()
		self.grahview = self.wTree.get_widget("vbox1")
		self.grahview.pack_start(self.canvas, True, True)
				
app = appGui()
gtk.main()
	

