Question

Coding in Python Add radio button options for filing status to the tax calculator program of...

Coding in Python

Add radio button options for filing status to the tax calculator program of Project 1. The user selects one of these options to determine the tax rate. The Single option’s rate is 20%. The Married option is 15%. The Divorced option is 10%. The default option is Single.

Be sure to use the field names provided in the comments in your starter code.

==================

Project 1 code:

# Initialize the constants

TAX_RATE = 0.20

STANDARD_DEDUCTION = 10000.0

DEPENDENT_DEDUCTION = 3000.0

from breezypythongui import EasyFrame


class TaxCalculator(EasyFrame):

    """Application window for the tax calculator."""

    def __init__(self):

        """Sets up the window and the widgets."""

        EasyFrame.__init__(self, title="Tax Calculator")

        # Label and field for the income

        self.addLabel(text="Income", row=0, column=0)

        self.incomeField = self.addFloatField(value=0.0, row=0, column=1)

        # Label and field for the number of dependents

        self.addLabel(text="Dependents", row=1, column=0)

        self.depField = self.addIntegerField(value=0, row=1, column=1)

        # The command button

        self.addButton(text="Compute", row=2, column=0, columnspan=2, command=self.computeTax)

        # Label and field for the tax

        self.addLabel(text="Total tax", row=3, column=0)

        self.taxField = self.addFloatField(value=0.0, row=3, column=1, precision=2)

    # The event handler method for the button

    def computeTax(self):

        """Obtains the data from the input fields and uses

        them to compute the tax, which is sent to the

        output field."""

        income = self.incomeField.getNumber()

        numDependents = self.depField.getNumber()

        tax = (income - STANDARD_DEDUCTION - numDependents*DEPENDENT_DEDUCTION) * TAX_RATE

        self.taxField.setNumber(tax)


def main():

    TaxCalculator().mainloop()


if __name__ == '__main__':

    main()

==================

My code for this exercise (which is incorrect):

# import tkinter for python gui elements

from tkinter import *

# function definition

def tax():

# assuming some amount to find the tax amount

amount = 35000

# if the selected option is 'single' then get 20% of assumed amount

if var.get() == 1:

tax_amt = 0.2 * amount

# if 'married' then 15% of assumed amount

elif var.get() == 2:

tax_amt = 0.15 * amount

# if divorced then 10% of assumed amount

elif var.get() == 3:

tax_amt = 0.1 * amount

# some display text

disp = "Taxable amount based on the selection is : " + str(tax_amt)

# set the label (which is created in the following steps) with the display text

label.config(text = disp)

# prepare a root window

root = Tk()

# name the root window

root.title("Tax calculator")

# mention the dimension of root window

root.geometry("300x200")

# initialize the variable as integer

var = IntVar()

# set the integer value to 1 i.e, first radio button selection by default

var.set(1)  

# create buttons and place them in window as required

R1 = Radiobutton(root, text = "Single", variable = var, value = 1)

R1.place(x=50,y=50)

R2 = Radiobutton(root, text = "Married", variable = var, value = 2)

R2.place(x=50,y=80)

R3 = Radiobutton(root, text = "Divorced", variable = var, value = 3)

R3.place(x=50,y=110)

# create button and call the function when this button clicked

calculate_button = Button(root, text="Calculate", command=tax)

calculate_button.place(x=50, y=140)

  

# create another button to close the window

quit_button = Button(root, text="Quit", command=root.quit)

quit_button.place(x=130, y=140)

# create a label and place it so that the display text in window is displayed as expected

label = Label(root)

label.place(x=20, y=20)

# call mainloop() to start the event

root.mainloop()

Homework Answers

Answer #1

Here is the code

from tkinter import *

def taxamount(income, percent):
  tax = income * (percent/100)
  return tax

def calculate():
  outputField.delete(0,'end')
  income = float(incomefield.get())
  dependents = int(dependentsfield.get())
  percent = p.get()
  tax = taxamount((income/dependents),percent)   #here i pass income as income/dependents
  output.set(str(tax))



root = Tk()
root.configure(background="white")
root.title("Tax Calculator")
root.resizable(1,0)
root.columnconfigure(0,weight=1)
root.columnconfigure(1,weight=1)

labelincome = Label(root, text="Income",background="white")
labelincome.grid(row=1,column=0, sticky=E)
labeldependents = Label(root, text="Dependents",background="white")
labeldependents.grid(row=2,column=0, sticky=E)

incomefield = Entry(root)
incomefield.grid(row=1,column=1,sticky=W)
dependentsfield = Entry(root)
dependentsfield.grid(row=2,column=1,sticky=W)

p=IntVar()
p.set(20)
Radiobutton(root,text="Single",variable=p,value=20).grid(row=1,column=3, sticky=W+E)
Radiobutton(root,text="Married",variable=p,value=15).grid(row=2,column=3, sticky=W+E)
Radiobutton(root, text="Divorced",variable=p,value=10).grid(row=3,column=3, sticky=W+E)

button = Button(root, text="Compute", command=calculate)
button.grid(row=3, columnspan=2)

labeltotaltax = Label(root, text="Total tax",background="white")
labeltotaltax.grid(row=4,column=0, sticky=E)
output = StringVar()
outputField = Entry(root, textvariable=output,justify=CENTER)
outputField.grid(row=4,column=1,sticky=W)
outputField.columnconfigure(0,weight=1)

root.mainloop()

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions