使用条目和按钮 TKinter、税务计算器更改变量

问题描述 投票:0回答:1

尝试制作一个税收计算器,我一直在努力研究如何制作它,以便可以使用输入框中的输入和确认输入的按钮来更改变量(工资)。我不确定这是我的布局还是我遗漏了一些东西(可能很明显)。

我确信这与错误代码的命令有关: 类型错误:int() 参数必须是字符串、类似字节的对象或实数,而不是“Entry” 运行时就会出现。

如果在代码本身中更改工资变量,那么一切似乎都会加起来,所以这只是如何将它们结合在一起。

这是我到目前为止所做的代码:

import tkinter as tk

salary = 1

window = tk.Tk()

title = tk.Label(text="Salary Calculator")
title.grid(column=1, row=0)

canvas = tk.Canvas(width=300, height=300, bg="black",)
canvas.grid(column=1, row=1)

gross_wage_title = tk.Label(text="Enter gross wage: £")
tax_band_title = tk.Label(text="Your Tax band is: ")
taxation_title = tk.Label(text="Amount to pay in tax: £")
yearly_net_wage_title = tk.Label(text="Your yearly net wage will be: £")
monthly_net_wage_title = tk.Label(text="Your monthly net wage will be: £")

gross_wage_title.grid(column=0, row=2, columnspan=2)
tax_band_title.grid(column=0, row=3, columnspan=2)
taxation_title.grid(column=0, row=4, columnspan=2)
yearly_net_wage_title.grid(column=0, row=5, columnspan=2)
monthly_net_wage_title.grid(column=0, row=6, columnspan=2)

def gross_salary():
    global salary
    salary = int(gross_wage_input)

gross_wage_input = tk.Entry()
gross_wage_input.grid(column=2, row=2)

calculate = tk.Button(text="Calculate", command=gross_salary)
calculate.grid(column=2, row=7)

# ------------------------------------------------------------------ #
salary = 0
if salary < 12570:
    tax = 0
    band = "No Rate"
    keep = salary
elif int(salary) in range(12570, 14732):
    tax = 19
    band = "Starter Rate"
    taxable = round((int(salary)/100) * tax, 2)
    keep = round(int(salary) - taxable, 2)
elif int(salary) in range(14733, 25688):
    tax = 20
    band = "Basic Rate"
    taxable = round((int(salary)/100) * tax, 2)
    keep = round(int(salary) - taxable, 2)
elif int(salary) in range(25689, 43662):
    tax = 21
    band = "Intermediate Rate"
    taxable = round((int(salary)/100) * tax, 2)
    keep = round(int(salary) - taxable, 2)
elif int(salary) in range(43663, 125140):
    tax = 42
    band = "Higher Rate"
    taxable = round((int(salary)/100) * tax, 2)
    keep = round(int(salary) - taxable, 2)
elif int(salary) > 125140:
    tax = 47
    band = "Additional Rate"
    taxable = round((int(salary)/100) * tax, 2)
    keep = round(int(salary) - taxable, 2)
else:
    print("No")

monthly_keeping = int(keep)/12
monthly_keep = round(monthly_keeping, 2)

print(f"""You tax band will be {band}. 
This Means you pay {tax}% in tax. 
You will keep £{keep} out of your £{salary} salary.
This will be a monthly net wage of £{monthly_keep}""")

tax_band_total = tk.Label(text=f"{band}")
taxation_total = tk.Label(text=f"{tax}")
yearly_net_wage_total = tk.Label(text=f"{keep}")
monthly_net_wage_total = tk.Label(text=f"{monthly_keep}")

tax_band_total.grid(column=2, row=3)
taxation_total.grid(column=2, row=4)
yearly_net_wage_total.grid(column=2, row=5)
monthly_net_wage_total.grid(column=2, row=6)

# ------------------------------------------------------------------ #


window.mainloop()

`

我想要的是将薪水添加到 Entry 小部件中,然后在按下“计算”按钮时将薪水变量更改为该数字。

python variables tkinter tax
1个回答
0
投票

您需要将主块中的计算移至

gross_salary()
函数中,并使用结果更新标签:

import tkinter as tk

window = tk.Tk()

title = tk.Label(window, text="Salary Calculator")
title.grid(column=1, row=0)

canvas = tk.Canvas(window, width=300, height=300, bg="black",)
canvas.grid(column=1, row=1)

gross_wage_title = tk.Label(window, text="Enter gross wage: £")
tax_band_title = tk.Label(window, text="Your Tax band is: ")
taxation_title = tk.Label(window, text="Amount to pay in tax: £")
yearly_net_wage_title = tk.Label(window, text="Your yearly net wage will be: £")
monthly_net_wage_title = tk.Label(window, text="Your monthly net wage will be: £")

gross_wage_title.grid(column=0, row=2, columnspan=2)
tax_band_title.grid(column=0, row=3, columnspan=2)
taxation_title.grid(column=0, row=4, columnspan=2)
yearly_net_wage_title.grid(column=0, row=5, columnspan=2)
monthly_net_wage_title.grid(column=0, row=6, columnspan=2)

def gross_salary():
    global salary
    try:
        salary = int(gross_wage_input.get().strip())

        if salary < 12570:
            tax = 0
            band = "No Rate"
            keep = salary
        elif int(salary) in range(12570, 14732):
            tax = 19
            band = "Starter Rate"
            taxable = round((int(salary)/100) * tax, 2)
            keep = round(int(salary) - taxable, 2)
        elif int(salary) in range(14733, 25688):
            tax = 20
            band = "Basic Rate"
            taxable = round((int(salary)/100) * tax, 2)
            keep = round(int(salary) - taxable, 2)
        elif int(salary) in range(25689, 43662):
            tax = 21
            band = "Intermediate Rate"
            taxable = round((int(salary)/100) * tax, 2)
            keep = round(int(salary) - taxable, 2)
        elif int(salary) in range(43663, 125140):
            tax = 42
            band = "Higher Rate"
            taxable = round((int(salary)/100) * tax, 2)
            keep = round(int(salary) - taxable, 2)
        elif int(salary) > 125140:
            tax = 47
            band = "Additional Rate"
            taxable = round((int(salary)/100) * tax, 2)
            keep = round(int(salary) - taxable, 2)
        else:
            print("No")
            return

        monthly_keeping = int(keep)/12
        monthly_keep = round(monthly_keeping, 2)

        print(f"""You tax band will be {band}.
        This Means you pay {tax}% in tax.
        You will keep £{keep} out of your £{salary} salary.
        This will be a monthly net wage of £{monthly_keep}""")

        # show result
        tax_band_total.config(text=f"{band}")
        taxation_total.config(text=f"{tax}")
        yearly_net_wage_total.config(text=f"{keep}")
        monthly_net_wage_total.config(text=f"{monthly_keep}")

    except ValueError as err:
        print(err)

gross_wage_input = tk.Entry(window)
gross_wage_input.grid(column=2, row=2)

calculate = tk.Button(window, text="Calculate", command=gross_salary)
calculate.grid(column=2, row=7)

tax_band_total = tk.Label(window)
taxation_total = tk.Label(window)
yearly_net_wage_total = tk.Label(window)
monthly_net_wage_total = tk.Label(window)

tax_band_total.grid(column=2, row=3)
taxation_total.grid(column=2, row=4)
yearly_net_wage_total.grid(column=2, row=5)
monthly_net_wage_total.grid(column=2, row=6)

window.mainloop()

请注意,在创建小部件时最好指定它们的主/父部件。

© www.soinside.com 2019 - 2024. All rights reserved.