1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| import tkinter as tk #import tkinter.messagebox as msb import tkinter.ttk as ttk
class TaxCalc(object): taxPoint = 5000
def __init__(self): self.top = tk.Tk() self.top.title("个人所得税计算器") sw,sh = self.top.winfo_screenwidth(),self.top.winfo_screenheight() ww,wh = 245,300 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="税前工资:") self.beforeTaxLable.grid(row=0,column=0) self.beforeTaxEntry = tk.Entry(self.top) self.beforeTaxEntry.grid(row=0,column=1,pady=10,padx=10)
self.insuranceLable= tk.Label(self.top,text="扣除的保险:") self.insuranceLable.grid(row=1,column=0) self.insuranceEntry = tk.Entry(self.top) self.insuranceEntry.grid(row=1,column=1,pady=10)
self.specailCutLable= tk.Label(self.top,text="专项抵扣总额:") self.specailCutLable.grid(row=2,column=0) self.specailCutEntry = tk.Entry(self.top) self.specailCutEntry.grid(row=2,column=1,pady=10)
#绑定事件,离开焦点或按回车就试着调用计算函数 self.specailCutEntry.bind("<FocusOut>",self.calcTax) self.specailCutEntry.bind("<Return>",self.calcTax)
self.sep = ttk.Separator(self.top, orient=tk.HORIZONTAL) self.sep.grid(row=3,column=0,columnspan=2,sticky="ew")
self.taxAmoutLable = tk.Label(self.top,text="应纳税额:") 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="应缴个税:") self.taxLable.grid(row=5,column=0) self.taxEntry = tk.Entry(self.top) self.taxEntry.grid(row=5,column=1,pady=10)
self.afterTaxLable = tk.Label(self.top,text="税后工资:") self.afterTaxLable.grid(row=6,column=0) self.afterTaxEntry = tk.Entry(self.top) self.afterTaxEntry.grid(row=6,column=1,pady=10)
self.calcBtn = ttk.Button(self.top,text="计算") self.calcBtn.grid(row=7,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()) insurance = float(self.insuranceEntry.get()) specailCut = float(self.specailCutEntry.get()) except ValueError as e: #空或非数字转成浮点时都会捕获,但不做响应 pass else: taxAmout = beforeTax - insurance - TaxCalc.taxPoint -specailCut taxAmout = taxAmout if taxAmout >0 else 0
if taxAmout < 3000: tax = taxAmout*0.03 elif taxAmout < 12000: tax = taxAmout*0.1-210 elif taxAmout < 25000: tax = taxAmout*0.2-1410 elif taxAmout < 35000: tax = taxAmout*0.25-2660 elif taxAmout < 55000: tax = taxAmout*0.3-4410 elif taxAmout < 80000: tax = taxAmout*0.35-7160 else: tax = taxAmout*0.45-15160
afterTax = beforeTax - insurance -tax
taxAmout = "{0:.2f}".format(taxAmout) tax = "{0:.2f}".format(tax) afterTax = "{0:.2f}".format(afterTax)
self.taxAmoutEntry.delete(0,tk.END) self.taxAmoutEntry.insert(0, taxAmout) self.taxEntry.delete(0,tk.END) self.taxEntry.insert(0,tax) self.afterTaxEntry.delete(0,tk.END) self.afterTaxEntry.insert(0, afterTax)
if __name__ == '__main__': s = TaxCalc()
|