鱼C论坛

 找回密码
 立即注册
查看: 1227|回复: 1

这是一个关于净工资计算器的程序,如何修改可以让它也可适应年薪和月薪

[复制链接]
发表于 2019-7-25 01:00:51 | 显示全部楼层 |阅读模式
10鱼币
这是一个关于净工资计算器的程序,现在是只适应于年薪。
1.如何修改可以让它也可适应月薪。并用单选按钮实现。
2. 如何设置异常处理。下面是我的程序。
import tkinter as tk
import tkinter.ttk as ttk

class TaxCalc(object):
    taxPoint = 12500

    def __init__(self):
        self.top = tk.Tk()
        self.top.title("Net Salary Calculator")
        sw,sh = self.top.winfo_screenwidth(),self.top.winfo_screenheight()
        ww,wh = 300,500
        self.top.geometry("{}x{}+{}+{}".format(ww,wh,(sw-ww)//2,(sh-wh)//2))
        self.top.resizable(0,0)
        self.createWdiget()
        self.top.wm_attributes('-topmost',1)  #主窗口置顶
        self.top.mainloop()

    def createWdiget(self):
        self.beforeTaxLable = tk.Label(self.top,text="gross salary:")
        self.beforeTaxLable.grid(row=0,column=0)
        self.beforeTaxEntry = tk.Entry(self.top)
        self.beforeTaxEntry.grid(row=0,column=1,pady=5,padx=5)
        
        self.pensionrateLable= tk.Label(self.top,text="pensionrate:")
        self.pensionrateLable.grid(row=1,column=0)
        self.pensionrateEntry = tk.Entry(self.top)
        self.pensionrateEntry.grid(row=1,column=1,pady=10)
        
        

        self.sep = ttk.Separator(self.top, orient=tk.HORIZONTAL)
        self.sep.grid(row=2,column=0,columnspan=2,sticky="ew")

        

        self.insuranceLable= tk.Label(self.top,text="insurance:")
        self.insuranceLable.grid(row=3,column=0)
        self.insuranceEntry = tk.Entry(self.top)
        self.insuranceEntry.grid(row=3,column=1,pady=10)

      
        self.taxAmoutLable = tk.Label(self.top,text="taxable income:")
        self.taxAmoutLable.grid(row=4,column=0)
        self.taxAmoutEntry = tk.Entry(self.top)
        self.taxAmoutEntry.grid(row=4,column=1,pady=10)

        self.taxLable = tk.Label(self.top,text="Tax:")
        self.taxLable.grid(row=5,column=0)
        self.taxEntry = tk.Entry(self.top)
        self.taxEntry.grid(row=5,column=1,pady=10)

        self.afterinsuranceLable = tk.Label(self.top,text="net salary:")
        self.afterinsuranceLable.grid(row=6,column=0)
        self.afterinsuranceEntry = tk.Entry(self.top)
        self.afterinsuranceEntry.grid(row=6,column=1,pady=10)
        
        self.pensionLable = tk.Label(self.top,text="pension contribution:")
        self.pensionLable.grid(row=7,column=0)
        self.pensionEntry = tk.Entry(self.top)
        self.pensionEntry.grid(row=7,column=1,pady=10)

        self.calcBtn = ttk.Button(self.top,text="calculator")
        self.calcBtn.grid(row=10,column=1,pady=10)
        self.calcBtn.bind("<Button-1>",self.calcTax)   #不能直接用Button的command参数绑定,commmand默认不传event参数

    def calcTax(self,event):     #做为事件的回调函数须要有event参数
        try:
            beforeTax = float(self.beforeTaxEntry.get())
            pensionrate = float(self.pensionrateEntry.get())
        except ValueError as e:   #空或非数字转成浮点时都会捕获,但不做响应
            pass
        else:
             taxAmout = beforeTax - TaxCalc.taxPoint if beforeTax > TaxCalc.taxPoint else 0     

             if beforeTax <= 12500:
                tax = beforeTax*0.00
             elif beforeTax <= 50000 and beforeTax>=12501:
                tax = (beforeTax-12500)*0.2
             elif beforeTax<=150000 and beforeTax>=50001:
                tax = (beforeTax-50000)*0.4+7500
             elif beforeTax> 150000:
                tax = (beforeTax-150000)*0.45+7500+40000
           
             afterTax = beforeTax - tax
             if beforeTax<=8632:
                insurance=beforeTax*0.00
             elif beforeTax <= 50000 and beforeTax>=8632.01:  
                insurance=(beforeTax-8632)*0.12
             elif beforeTax> 150000:
                insurance=(beforeTax-50000)*0.02+4964.16
             pension=beforeTax* pensionrate   
             afterinsurance=afterTax - insurance- pension

             insurance = "{0:.2f}".format(insurance)
             tax = "{0:.2f}".format(tax)
             afterinsurance = "{0:.2f}".format(afterinsurance)
             taxAmout = "{0:.2f}".format(taxAmout)
             pension = "{0:.2f}".format(pension)
            
             self.insuranceEntry.delete(0,tk.END)
             self.insuranceEntry.insert(0, insurance)
             self.taxEntry.delete(0,tk.END)
             self.taxEntry.insert(0,tax)
             self.afterinsuranceEntry.delete(0,tk.END)
             self.afterinsuranceEntry.insert(0, afterinsurance)
             self.taxAmoutEntry.delete(0,tk.END)
             self.taxAmoutEntry.insert(0, taxAmout)
             self.pensionEntry.delete(0,tk.END)
             self.pensionEntry.insert(0,pension)

if __name__ == '__main__':
    s = TaxCalc()

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-25 10:01:34 | 显示全部楼层
用try做异常处理,你既然会年薪,月薪为什么不会
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-11 10:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表