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

huangapple go评论60阅读模式
英文:

How do I handle multiple returns from a function called via a PYTHON Tkinter button?

问题

以下是您要翻译的代码部分:

def openWagesWindow():
    WagesWindow = tk.Tk()
    WagesWindow.title("工资")
    WagesWindow.geometry("800x600")
    windowlabel = tk.Label(WagesWindow, text="工资单")
    windowlabel.pack()
    
    Grosslabel = tk.Label(WagesWindow, text='总工资')
    Grosslabel.pack()
    Grossentry = tk.Entry(WagesWindow)
    Grossentry.insert(0, int(0))
    Grossentry.pack()

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

    NILabel = tk.Label(WagesWindow, text='国民保险')
    NILabel.pack()
    NIentry = tk.Entry(WagesWindow)
    NIentry.pack()

    PensionLabel = tk.Label(WagesWindow, text='养老金')
    PensionLabel.pack()
    Pensionentry = tk.Entry(WagesWindow)
    Pensionentry.pack()

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

    NetPaylabel = tk.Label(WagesWindow, text='净工资')
    NetPaylabel.pack()
    NetPayentry = tk.Entry(WagesWindow)
    NetPayentry.pack()

    backbutton = tk.Button(WagesWindow, text="返回", command=WagesWindow.destroy)
    backbutton.place(x=350, y=350)
    
    calcbutton = tk.Button(WagesWindow, text="计算", 
      command=lambda: CalcWages(Grossentry.get()))
    calcbutton.place(x=400, y=350)
    calcbutton = tk.Button(WagesWindow, text="计算", 
    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]

希望这有所帮助!

英文:

PYTHON Tkinter buttons which call a function.

I have a function run from a main menu which creates a data entry form for payroll.

I have a button on that form which takes the gross pay figure from the payroll form and passes it into a function to work out deductions like tax, pension etc and ultimately net pay. This deductions and net pay then need to be displayed on the form, bit like a wage slip.

I can pass the multiple values back ok but how can I get those separate values populated onto the form in the correct fields? I am a novice programmer.

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]

答案1

得分: 1

这是您提供的代码的翻译部分:

所以在这里有一些问题首先是如何处理多个返回”,因为函数可以返回一种可迭代的数据结构其中包含多个值就像您的函数一样通常这些东西以元组而不是列表的形式返回因为它是固定大小的不可变的等等因此您可以单独或使用一个变量捕获多个返回值然后拆分它请注意在这里的多个位置元组的括号是可选的

示例

# 多个返回
nums = [9, -1, 3, 4, 12, 44, -8, 10]

def max_min(number_list):
    """返回一个数字集合的最大值和最小值的元组"""
    max_value = max(number_list)
    min_value = min(number_list)
    return min_value, max_value

# 示例1:分别捕获最小值和最大值
nums_min, nums_max = max_min(nums)
print(f'最小值为{nums_min},最大值为{nums_max}')

# 示例2:也许我只想要最大值。下划线是合法的变量名,通常表示给读者的提示,它是一个“丢弃”的变量。
_, nums_max = max_min(nums)
print(f'最大值为{nums_max}')

# 示例3:将元组作为元组捕获并拆分...
results = max_min(nums)
print(f'最小值为{results[0]},最大值为{results[1]}')

输出

最小值为-8最大值为44
最大值为44
最小值为-8最大值为44

接下来是tkinter部分... 您可以使用上述策略来捕获多个输出并更新窗口您可以将我下面要做的事情放在一个仅更新窗口的函数中但我展示了两个函数只是为了说明一下

一些附注

1. 我不是tkinter专家但我认为您想要将输出放在`tk.label`因为它们是输出您不需要任何数据输入而且这样更容易我确信您可以调整格式使其看起来更漂亮...这不是我的强项

2. 为了在窗口变量上具有可见性您需要从定义它们的相同范围中访问它们在您的情况下这是在主函数内部所以我将您的辅助函数移动到内部函数可能有更好的结构可用您可以查看一些tk示例以获取其他想法这样可以工作并说明了重点

代码

```python
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)   # 多个返回值应该是元组,而不是列表

    def update_windows(gross_pay):
        # 使用包含多个变量的元组或只包含一个变量的元组来捕获多个返回值,稍后可以拆分它
        (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("工资")
    WagesWindow.geometry("800x600")
    windowlabel = tk.Label(WagesWindow, text="工资单")
    windowlabel.pack()
                
    Grosslabel = tk.Label(WagesWindow, text='总工资')
    Grosslabel.pack()
    Grossentry = tk.Entry(WagesWindow)
    Grossentry.insert(0, int(0))
    Grossentry.pack()

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

    NILabel = tk.Label(WagesWindow, text='国民保险')
    NILabel.pack()
    NIentry = tk.Label(WagesWindow)
    NIentry.pack()

    PensionLabel = tk.Label(WagesWindow, text='养老金')
    PensionLabel.pack()
    Pensionentry = tk.Label(WagesWindow)
    Pensionentry.pack()

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

    NetPaylabel = tk.Label(WagesWindow, text='实发工资')
    NetPaylabel.pack()
    NetPayentry = tk.Label(WagesWindow)
    NetPayentry.pack()

    backbutton = tk.Button(WagesWindow, text="返回", command=WagesWindow.destroy)
    backbutton.place(x=350, y=350)
        
    calcbutton = tk.Button(WagesWindow, text="计算", 
      command=lambda: update_windows(Grossentry.get()))
    calcbutton.place(x=400, y=350)

    WagesWindow.mainloop()

openWagesWindow()
英文:

So you have a couple questions wrapped up in this. The first one is how to deal with "multiple returns" as a function can return some kind of iterable data structure with multiple values like yours does. Typically, these things are returned in a tuple (not a list) because it is fixed size, immutable, etc. So you can catch multiple returns either individually or with one variable and split it up. Note that in several spots here, the parens for the tuple are optional

Example:

# 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]}')

Output:

the min is -8 and the max is 44
the max value is 44
the min is -8 and the max is 44

On to the tkinter.... You can employ the strategy above to catch the multiple outputs and update the windows. You could have done what I'm doing below in 1 function that just updates the windows directly, but I showed 2 just to make the point.

A couple of side notes:

  1. I'm not a tkinter expert, but I think you want to put your output in tk.labels because they are output and you aren't looking for any data entry, and it's easier. I'm sure you can monkey with the formatting to make it look prettier... not my forte.

  2. In order to have visibility on the window variables, you need to be accessing them from the same scope in which they are defined. In your case, this is inside your main function, so I moved your "helper functions" to be internal functions. There is likely a better structure to be had, and you might review some tk examples for other ideas. This works and makes the point.

Code

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()

huangapple
  • 本文由 发表于 2023年5月23日 00:04:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定