r/learnpython • • Jun 27 '23

Please help me with error

Question:
Income Tax Calculator (hypothetical): Assume the income tax is calculated using the following rules:

  1. Salary/Pension income:  taxed at the flat rate of 15% of annual net salary.

  2. Capital gain income: long term capital gain is taxed at 10% while short-term capital gain is taxed at 33%.

  3. All other income: 15% tax rate for the first $100,000 and 26% for any income above $100,000

  4. Senior Citizens: Senior citizens get a 20% exemption (i.e. their taxes are reduced by 20%).
    Write a Python GUI application (using tkinter) that prompts the user to enter income for each of the source category listed above and displays their income tax. Use a radio button for the Senior Citizenship status.
    -----------------------------------------------------------------------
    My answer:
    from tkinter import*
    class incomeTax():
    def __init__(self):
    window=Tk()
    window.title=("Income Tax Calculator")
    frame1=Frame(window)
    frame1.pack()
    Label(frame1, text="salary/pension income:").grid(row=1, column=1, sticky=W)
    Label(frame1, text="capital gain income:").grid(row=2, column=1, sticky=W)
    self.capital=StringVar()
    rbcapital1=Radiobutton(frame1, text="long term gain",variable=self.capital, value=1, command=self.processradiobutton)
    rbcapital2=Radiobutton(frame1, text="short term gain",variable=self.capital, value=2, command=self.processradiobutton)
    rbcapital1.grid(row=2, column=2)
    rbcapital2.grid(row=3, column=2)
    self.senior_citizens=StringVar()
    btseniorcitizens=Button(frame1, text="senior citizens",variable=self.senior_citizens,command=self.processcheckbutton)
    btseniorcitizens.grid(row=5, column=1, sticky=W)
    Label(frame1, text="all other income:").grid(row=4, column=1, sticky=W)
    Label(frame1, text="total income tax:").grid(row=6, column=1, sticky=W)

frame2=Frame(window)
frame2.pack()
salaryVar=StringVar()
entrysalary=Entry(frame2,variable=salaryVar).grid(row=1, column=3, sticky=E)
capitalVar=StringVar()
entrycapital=Entry(frame2,variable=capitalVar).grid(row=3, column=3, sticky=E)
otherVar=StringVar()
entryother=Entry(frame2,variable=otherVar).grid(row=4, column=3, sticky=E, command=self.otherincometax)
totalVar=StringVar()
entrtytotal=Entry(frame2,variable=totalVar, command=self.total).grid(row=6, column=3, sticky=E)
btcalculation=Button(window, text="calculate income tax", command=self.processbutton)

window.mainloop()
def processradiobutton(self):
print("long term gain" if self.capital.get()==1 else "short term gain", "is selected")

def processcheckbutton(self):
print("check button is",)+("checked" if self.senior_citizens.get()==1 else "unchecked")
def otherincometax(self):
if otherVar.get()>100000:
self.otherincometax=(float(otherVar.get())-100000)*0.26+15000
else:
self.otherincometax=float(otherVar.get())*0.15
return self.otherincometax
def processbutton(self):
if self.capital.get()==1 and self.senior_citizens.get()==1:
self.processbutton=0.8*(float(salaryVar.get())*0.15+ float(capitalVar.get())*0.1+float(otherincometax).get())
elif self.capital.get()==2 and self.senior_citizens.get()==1:
self.processbutton=0.8*(float(salaryVar.get())*0.15+float(capitalVar.get())*0.33+float(otherincometax).get())
elif self.capital.get()==1 and self.senior_citizens.get()==2:
self.processbutton=float(salaryVar.get())*0.15+ float(capitalVar.get())*0.1+float(otherincometax.get())
else:
self.processbutton=float(salaryVar.get())*0.15+float(capitalVar.get())*0.33+float(otherincometax.get())
return self.processbutton

def total(self):
print(self.processbutton.get())
incomeTax()

--------------------------------------------------------
please help me with error of the above program:
Traceback (most recent call last):
File "C:/Users/13559/OneDrive/桌面/module3.py", line 66, in <module>
incomeTax()
File "C:/Users/13559/OneDrive/桌面/module3.py", line 18, in __init__
btseniorcitizens=Button(frame1, text="senior citizens",variable=self.senior_citizens,command=self.processcheckbutton)
File "C:\Users\13559\AppData\Local\Programs\Python\Python311\Lib\tkinter__init__.py", line 2706, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Users\13559\AppData\Local\Programs\Python\Python311\Lib\tkinter__init__.py", line 2628, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-variable"

0 Upvotes

5 comments sorted by

View all comments

2

u/m0us3_rat Jun 27 '23

you first design the logic. that should work independently of GUI.

then you build the GUI on top.

this type of mixmash of code is horrible to debug. or work with. or anything.

i'd suggest refactor it in the proper way.