Tax Calculator--Tkinter

Marcus Gnaß gonatan at gmx.de
Mon Nov 9 19:24:10 CET 2009


Someone Something wrote:
> > from Tkinter import *;

Try to avoid this. Better import Tkinter. And don't forget to import
Tkconstants too!

> > rate=Frame(root)
> > income=Frame(root)
> > result=Frame(root)

Why do you use three frames? You only need one. And you can make your
class TaxCalc inherit from Tkinter.Frame ...

> > The thing is, that even if I put "12" in the result text field, get
> > returns an empty string. How can I fix this?

I haven't found the reason for that, but this should work. I also added
MRABs version of printResult().

import Tkinter, Tkconstants

class TaxCalc(Tkinter.Frame):

	def __init__(self, root):
		
		Tkinter.Frame.__init__(self, root)

		Tkinter.Button(self,
			text='Enter tax rate',
			command=self.getRate).pack()	

		self.rate=Tkinter.Entry(self)
		self.rate.pack()

		Tkinter.Button(self,
			text='Enter income',
			command=self.getIncome).pack()	

		self.income=Tkinter.Entry(self)
		self.income.pack()

		Tkinter.Button(self,
			text='Get result',
			command=self.printResult).pack()	

		self.result=Tkinter.Entry(self)
		self.result.pack()

		self.pack()

	def getRate(self):
		print "srate: ", self.rate.get()
	
	def getIncome(self):
		print "sincome: ", self.income.get()

	def printResult(self):
		try:
			rate = float(self.rate.get())
			income = float(self.income.get())
			result = ((100.0 - rate) / 100.0) * income
			self.result.insert(Tkconstants.END, str(result))
		except ValueError:
			print "Clear everything and start again."
			print "Don't fool around with me."

root=Tkinter.Tk()
MyCalc=TaxCalc(root)
root.mainloop()




More information about the Python-list mailing list