Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Tax Calculator--Tkinter

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


fordhaivat at gmail

Nov 8, 2009, 4:22 PM

Post #1 of 3 (200 views)
Permalink
Tax Calculator--Tkinter

I'm writing a simple tax calculator with Tkinter (just for fun).
Here's my current code:

from Tkinter import *;

class TaxCalc:
def __init__(self, root):

rate=Frame(root)
rate.pack()

income=Frame(root)
income.pack()

result=Frame(root)
result.pack()

self.rate=Entry(rate);
self.rate.pack();
self.enterr=Button(rate)
self.enterr['text']="Enter tax rate";
self.enterr['command']=self.getRate;
self.enterr.pack()

self.income=Entry(income);
self.income.pack();
self.enteri=Button(income);
self.enteri['text']="Enter income";
self.enterr['command']=self.getIncome;
self.enteri.pack();

self.result=Entry(result);
self.result.pack();
self.entere=Button(result);
self.entere['text']="Get result";
self.entere['command']=self.printResult;
self.entere.pack();

def getRate(self):
srate=self.rate.get();
print "srate: ", srate;

def getIncome(self):
sincome=self.income.get();
print "sincome: ", sincome;

def printResult(self):
if self.nrate is None | self.nincome is None:
print "Clear everything and start again.";
print "Don't fool around with me.";
else:
self.nresult=float(((100-self.nrate)/100)*self.nincome);
self.result.insert(END, str(self.nresult));

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

The thing is, that even if I put "12" in the result text field, get
returns an empty string. How can I fix this?
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Nov 8, 2009, 4:50 PM

Post #2 of 3 (193 views)
Permalink
Re: Tax Calculator--Tkinter [In reply to]

Someone Something wrote:
> I'm writing a simple tax calculator with Tkinter (just for fun).
> Here's my current code:
>
[snip]

> def printResult(self):
> if self.nrate is None | self.nincome is None:

There's no such attribute as nrate or nincome.

Also, "|" is the bitwise operator. You probably intended "or" instead.

> print "Clear everything and start again.";
> print "Don't fool around with me.";
> else:
> self.nresult=float(((100-self.nrate)/100)*self.nincome);
> self.result.insert(END, str(self.nresult));
>
Try this instead:

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

--
http://mail.python.org/mailman/listinfo/python-list


gonatan at gmx

Nov 9, 2009, 10:24 AM

Post #3 of 3 (179 views)
Permalink
Re: Tax Calculator--Tkinter [In reply to]

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()

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.