Binding Function with Layout - Help Please!!!

+1 Sunday Akinduro · February 5, 2015
Hi Bucky,

Please I need help. I am a starter.

I defined the function below and it works for its purpose. I attempted to bind the function with the layout I created but I am not getting the desired result.

I would like that the user enter the parameters of the function in the Entry field created in the GUI and by clicking the Monthly Tax Payable button, the user will see the result.

Thanks in advance for your help.

from tkinter import *

root = Tk()
root.title('Paye Calculator')
frame = Frame(root)
frame.grid()


def calculate_paye(gross_pay, pension, life_assur):
    annual_tax_payable = (12 * float(gross_pay)) - (
        200000 + (0.2 * 12 * float(gross_pay)) + (12 * float(pension + life_assur)))
    if annual_tax_payable <= 0:
        monthly_tax_payable = (0.01 * 12 * gross_pay) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 0 < annual_tax_payable <= 300000:
        monthly_tax_payable = (0.07 * annual_tax_payable) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 300000 < annual_tax_payable <= 600000:
        monthly_tax_payable = ((300000 * 0.07) + ((annual_tax_payable - 300000) * 0.11)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 600000 < annual_tax_payable <= 1100000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + ((annual_tax_payable - 600000) * 0.15)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 1100000 < annual_tax_payable <= 1600000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + (500000 * 0.15) + (
            (annual_tax_payable - 1100000) * 0.19)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 1600000 < annual_tax_payable <= 3200000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + (500000 * 0.15) + (500000 * 0.19) + (
            (annual_tax_payable - 1600000) * 0.21)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif annual_tax_payable > 3200000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + (500000 * 0.15) + (500000 * 0.19)+(1600000 * 0.21) + (
            (annual_tax_payable - 3200000) * 0.24)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    else:
        print('No tax due')

calculate_paye(50000, 4000, 0)

gross_payEntry = Entry(frame, text="Enter Monthly Gross Pay")
gross_payEntry.grid(row=0, column=1)
gross_payLabel = Label(frame, text="Monthly Gross Pay=NGN")
gross_payLabel.grid(row=0, column=0, sticky=E)

pensionEntry = Entry(frame, text='Monthly Pension Contribution')
pensionEntry.grid(row=1, column=1)
pensionLabel = Label(frame, text="Monthly Pension=NGN")
pensionLabel.grid(row=1, column=0, sticky=E)

life_assuranceEntry = Entry(frame, text='Monthly Life Assurance Contribution')
life_assuranceEntry.grid(row=2, column=1)
life_assuranceLabel = Label(frame, text="Monthly Life Assurance Contribution=NGN")
life_assuranceLabel.grid(row=2, column=0, sticky=E)

monthly_tax_payableButton = Button(frame, text="Monthly Tax Payable", command=calculate_paye(80000, 5000, 0))
monthly_tax_payableButton.grid(row=3, column=1, sticky=E)

root.mainloop()


Post a Reply

Replies

Oldest  Newest  Rating
0 Nishant Kumar Thakur · February 6, 2015
I suppose this would help:-

from Tkinter import *

root = Tk()
root.title('Pay Calculator')
frame = Frame(root)
frame.grid()


def calculate_pay(gross_pay, pension, life_assur):
    annual_tax_payable = (12 * float(gross_pay)) - (
        200000 + (0.2 * 12 * float(gross_pay)) + (12 * float(pension + life_assur)))
    if annual_tax_payable <= 0:
        monthly_tax_payable = (0.01 * 12 * gross_pay) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 0 < annual_tax_payable <= 300000:
        monthly_tax_payable = (0.07 * annual_tax_payable) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 300000 < annual_tax_payable <= 600000:
        monthly_tax_payable = ((300000 * 0.07) + ((annual_tax_payable - 300000) * 0.11)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 600000 < annual_tax_payable <= 1100000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + ((annual_tax_payable - 600000) * 0.15)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 1100000 < annual_tax_payable <= 1600000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + (500000 * 0.15) + (
            (annual_tax_payable - 1100000) * 0.19)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif 1600000 < annual_tax_payable <= 3200000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + (500000 * 0.15) + (500000 * 0.19) + (
            (annual_tax_payable - 1600000) * 0.21)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    elif annual_tax_payable > 3200000:
        monthly_tax_payable = ((300000 * 0.07) + (300000 * 0.11) + (500000 * 0.15) + (500000 * 0.19)+(1600000 * 0.21) + (
            (annual_tax_payable - 3200000) * 0.24)) / 12
        print(format(monthly_tax_payable, "8,.2f"))
    else:
        print('No tax due')
        monthly_tax_payable = 0
        
    return format(monthly_tax_payable, "8,.2f")

def getInputAndSetOutputOnUI():
    gross_pay = float(gross_payEntry.get())
    pension = float(pensionEntry.get())
    life_assurance_contri = float(life_assuranceEntry.get())
    monthly_tax_payable = calculate_pay(gross_pay, pension, life_assurance_contri)
    resultString.set("Monthly Tax : "+ monthly_tax_payable)
    
    

gross_payEntry = Entry(frame, text="Enter Monthly Gross Pay")
gross_payEntry.grid(row=0, column=1)
gross_payLabel = Label(frame, text="Monthly Gross Pay=NGN")
gross_payLabel.grid(row=0, column=0, sticky=E)

pensionEntry = Entry(frame, text='Monthly Pension Contribution')
pensionEntry.grid(row=1, column=1)
pensionLabel = Label(frame, text="Monthly Pension=NGN")
pensionLabel.grid(row=1, column=0, sticky=E)

life_assuranceEntry = Entry(frame, text='Monthly Life Assurance Contribution')
life_assuranceEntry.grid(row=2, column=1)
life_assuranceLabel = Label(frame, text="Monthly Life Assurance Contribution=NGN")
life_assuranceLabel.grid(row=2, column=0, sticky=E)

monthly_tax_payableButton = Button(frame, text="Monthly Tax Payable", command=getInputAndSetOutputOnUI)
monthly_tax_payableButton.grid(row=3, column=1, sticky=E)

resultString = StringVar()
resultString.set("Enter details and click button to get Monthly Tax")
resultField = Label(frame, textvariable=resultString)
resultField.grid(row=3, column=0, sticky=E)

root.mainloop()
  • 1

Python

107,112 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator