英文:
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:
-
I'm not a tkinter expert, but I think you want to put your output in
tk.label
s 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. -
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论