如何处理通过PYTHON Tkinter按钮调用的函数的多个返回?

zphenhs4  于 2023-05-23  发布在  Python
关注(0)|答案(1)|浏览(160)

PYTHON Tkinter按钮,调用函数。
我有一个功能运行从一个主菜单,创建一个数据输入形式的工资单。
我有一个按钮,该表格采取的总工资数字从工资单的形式,并通过它到一个功能,以制定扣除如税,养老金等,并最终净工资。然后需要在表单上显示这些扣除额和净工资,有点像工资单。
我可以将多个值传递回去,但是如何将这些单独的值填充到表单的正确字段中呢?我是一个程序员新手。

def openWagesWindow():
    WagesWindow=tk.Tk()
    WagesWindow.title("Wages")
    WagesWindow.geometry("800x600")
    windowlabel = tk.Label(WagesWindow, text ="Payroll")
                windowlabel.pack()
                
    Grosslabel = tk.Label(WagesWindow, text= 'Gross Pay')
    Grosslabel.pack()
    Grossentry = tk.Entry(WagesWindow)
    Grossentry.insert(0,int(0))
    Grossentry.pack()

    TaxLabel = tk.Label(WagesWindow, text= 'Tax')
    TaxLabel.pack()
    Taxentry = tk.Entry(WagesWindow)
    Taxentry.pack()

    NILabel = tk.Label(WagesWindow, text= 'National Insurance')
    NILabel.pack()
    NIentry = tk.Entry(WagesWindow)
    NIentry.pack()

    PensionLabel = tk.Label(WagesWindow, text= 'Pension')
    PensionLabel.pack()
    Pensionentry = tk.Entry(WagesWindow)
    Pensionentry.pack()

    Deductlabel = tk.Label(WagesWindow, text= 'Deductions')
    Deductlabel.pack()
    Deductentry = tk.Entry(WagesWindow)
    Deductentry.pack()

    NetPaylabel = tk.Label(WagesWindow, text= 'Net Pay')
    NetPaylabel.pack()
    NetPayentry = tk.Entry(WagesWindow)
    NetPayentry.pack()

    backbutton = tk.Button(WagesWindow, text= "Back", command= WagesWindow.destroy)
    backbutton.place(x=350, y=350)
        
    calcbutton = tk.Button(WagesWindow, text= "Calculate", 
      command = lambda:CalcWages(Grossentry.get()))
    calcbutton.place(x=400, y=350)
    calcbutton = tk.Button(WagesWindow, text= "Calculate", 
    command = lambda:CalcWages(Grossentry.get())) 

def CalcWages(Grosspay):
   GP = int(Grosspay) 
   TX = GP *0.2 
   NI = GP *0.14 
   PNS = GP * 0.08 
   DEDUCT= TX+NI+PNS 
   NET= GP-DEDUCT 
   return[TX,NI,PNS,DEDUCT,NET]
bvn4nwqk

bvn4nwqk1#

所以你有几个问题要问。第一个是如何处理“多个返回”,因为函数可以像您的函数一样返回具有多个值的某种可迭代数据结构。通常情况下,这些东西以元组(而不是列表)的形式返回,因为它是固定大小的,不可变的等。因此,您可以单独或使用一个变量捕获多个返回并将其拆分。注意,在这里的几个地方,元组的括号是可选的

示例:
# multiple return

nums = [9, -1, 3, 4, 12, 44, -8, 10]

def max_min(number_list):
    """return a tuple of the max and min values from a collection of numbers"""
    max_value = max(number_list)
    min_value = min(number_list)
    return min_value, max_value

# example 1: catch both max and min in 2 variables
nums_min, nums_max = max_min(nums)
print(f'the min is {nums_min} and the max is {nums_max}')

# example 2: maybe I just want the max.  an underscore is a legal variable name
#            and usually indicates to the reader that it is a "throw-away"
_, nums_max = max_min(nums)
print(f'the max value is {nums_max}')

# example 3:  catch the tuple as a tuple and split it up...
results = max_min(nums)
print(f'the min is {results[0]} and the max is {results[1]}')
输出:
the min is -8 and the max is 44
the max value is 44
the min is -8 and the max is 44

在Tkinter…您可以使用上面的策略来捕获多个输出并更新窗口。你可以在下面的1个函数中直接更新窗口,但我展示了2个函数只是为了说明这一点。
一些旁注:
1.我不是一个tkinterMaven,但我认为你想把你的输出放在tk.label中,因为它们是输出,你不需要任何数据输入,而且它更容易。我敢肯定你可以猴子与格式,使它看起来更漂亮...不是我的强项。
1.为了具有窗口变量的可见性,您需要从定义它们的同一范围访问它们。在你的例子中,这是在你的主函数中,所以我把你的“助手函数”移到了内部函数中。可能会有更好的结构,您可以查看一些tk示例以获得其他想法。这是可行的,并提出了这一点。

代码
import tkinter as tk

def openWagesWindow():
    def CalcWages(gross_pay):
       GP = int(gross_pay) 
       print(GP)
       TX = GP *0.2 
       NI = GP *0.14 
       PNS = GP * 0.08 
       DEDUCT= TX+NI+PNS 
       NET= GP-DEDUCT 
       return (TX,NI,PNS,DEDUCT,NET)   # multiple returns should be tuple, not list

    def update_windows(gross_pay):
        # catch a multiple return with a tuple of variables or just 1 variable 
        # which will be a tuple and split it up later
        (tx, ni, pns, deduct, net) = CalcWages(gross_pay)
        Taxentry['text'] = str(tx)
        NIentry['text'] = str(ni)
        Pensionentry['text'] = str(pns)
        Deductentry['text'] = str(deduct)
        NetPayentry['text'] = str(net)

    WagesWindow=tk.Tk()
    WagesWindow.title("Wages")
    WagesWindow.geometry("800x600")
    windowlabel = tk.Label(WagesWindow, text ="Payroll")
    windowlabel.pack()
                
    Grosslabel = tk.Label(WagesWindow, text= 'Gross Pay')
    Grosslabel.pack()
    Grossentry = tk.Entry(WagesWindow)
    Grossentry.insert(0,int(0))
    Grossentry.pack()

    TaxLabel = tk.Label(WagesWindow, text= 'Tax')
    TaxLabel.pack()
    Taxentry = tk.Label(WagesWindow)
    Taxentry.pack()

    NILabel = tk.Label(WagesWindow, text= 'National Insurance')
    NILabel.pack()
    NIentry = tk.Label(WagesWindow)
    NIentry.pack()

    PensionLabel = tk.Label(WagesWindow, text= 'Pension')
    PensionLabel.pack()
    Pensionentry = tk.Label(WagesWindow)
    Pensionentry.pack()

    Deductlabel = tk.Label(WagesWindow, text= 'Deductions')
    Deductlabel.pack()
    Deductentry = tk.Label(WagesWindow)
    Deductentry.pack()

    NetPaylabel = tk.Label(WagesWindow, text= 'Net Pay')
    NetPaylabel.pack()
    NetPayentry = tk.Label(WagesWindow)
    NetPayentry.pack()

    backbutton = tk.Button(WagesWindow, text= "Back", command= WagesWindow.destroy)
    backbutton.place(x=350, y=350)
        
    calcbutton = tk.Button(WagesWindow, text= "Calculate", 
      command = lambda:update_windows(Grossentry.get()))
    calcbutton.place(x=400, y=350)
    # calcbutton = tk.Button(WagesWindow, text= "Calculate", 
    # command = lambda:CalcWages(Grossentry.get())) 
    
    WagesWindow.mainloop()

openWagesWindow()

相关问题