python - 在 lambda 函数中将多个函数作为参数传递不起作用
问题描述
这两个函数在计算糖果价格的 lamba 函数中不能作为 args 工作
def mysweets(): b = v.get( ) # get the value of v set cost=int(mysweets_price_list[b]) #price_display print(cost) def quantity_sweets(): q = int(spinbox1.get()) print(q) price = lambda b, q : b * q # final price to be displayed in myLabel_3 print(price(b, q))
我已经尝试过嵌套函数,但它们不起作用,请帮助任何人
from tkinter import *
myGui = Tk()
myGui.geometry('450x450+200+200')
myGui.title('Auto Sweet Dispenser')
price_display = ""
b = 0
#a = 0
q = 0
mysweets_price_list = {1 :9.00,
2 :7.50,
} # dict for the sweet prices
def mysweets():
b = v.get( ) # get the value of v set
cost=int(mysweets_price_list[b]) #price_display
print(cost)
def quantity_sweets():
q = int(spinbox1.get())
print(q)
price = lambda b, q : b * q # final price to be displayed in myLabel_3
print(price(b, q))
v =IntVar()
price =IntVar()
v.set(1)
myLabel = Label(myGui,text = 'Choose your sweets',font = 14, fg ='brown').place(x=140,y=55)#grid(row=3,column=10,sticky = 'e')
myRadio_1 = Radiobutton(myGui,text = 'Mints',variable = v, value = 1, command = mysweets).place(x= 160, y = 100)
myRadio_2 = Radiobutton(myGui,text = 'Nut log',variable = v, value = 2, command = mysweets).place(x= 160, y = 120)
myLabel_2 = Label(myGui,text = 'Select Quantity',font = 12, fg ='brown').place(x=160,y=160)#grid(row=3,column=10,sticky = 'e')
myLabel_3 = Label(myGui,textvariable = price ,font = "Times 14 bold",width = 14, fg ='white', bg= 'blue' ,relief = RAISED).place(x=160,y=220)#grid(row=3,column=10,sticky = 'e')
spinbox1 = Spinbox(myGui,from_=1,to = 6,command = quantity_sweets, state = NORMAL)
spinbox1.place(x=160,y=180)#
myGui.mainloop()
该代码有效,只是由于 lambda 函数不工作而没有显示价格。
解决方案
您在这里不需要 lambda(通常 lambda 应该非常罕见)。您只需要一个函数即可获取所有数据、进行计算并更新标签。像这样:
from tkinter import *
myGui = Tk()
myGui.geometry('450x450+200+200')
myGui.title('Auto Sweet Dispenser')
mysweets_price_list = {1 :9.00,
2 :7.50,
} # dict for the sweet prices
def calculate():
b = v.get( ) # get the value of v set
cost=mysweets_price_list[b] #price
q = int(spinbox1.get()) # get quantity.
final_price = cost * q # final price to be displayed
price.set(final_price)
v =IntVar(value=1) # set initial value to 1
price = IntVar()
Label(myGui,text = 'Choose your sweets',font = 14, fg ='brown').place(x=140,y=55)#grid(row=3,column=10,sticky = 'e')
Radiobutton(myGui,text = 'Mints',variable = v, value = 1, command = calculate).place(x= 160, y = 100)
Radiobutton(myGui,text = 'Nut log',variable = v, value = 2, command = calculate).place(x= 160, y = 120)
Label(myGui,text = 'Select Quantity',font = 12, fg ='brown').place(x=160,y=160)#grid(row=3,column=10,sticky = 'e')
Label(myGui,textvariable = price ,font = "Times 14 bold",width = 14, fg ='white', bg= 'blue' ,relief = RAISED).place(x=160,y=220)#grid(row=3,column=10,sticky = 'e')
spinbox1 = Spinbox(myGui,from_=1,to = 6,command = calculate, state = NORMAL)
spinbox1.place(x=160,y=180)
calculate() # do the calculation at boot
myGui.mainloop()
此外,非常重要的是要知道,如果您这样做,name = Widget().place()
那么名称将被设置None
为无用。你需要做
name = Widget()
name.grid() # or pack() or place()
或者
Widget().grid() # or pack() or place()
不要混合这两种风格!2-line的样式要好很多,和我们平时用的一样。
推荐阅读
- reactjs - 在 React 中使用带有 JSS 和 Material-UI 的三元运算符
- c# - c# 比较字符串有点宽大
- rust - 如何将 i32 和 f32 转换为 [u8; 4]?
- rest - 我应该同时实现 PUT 和 PATCH 路由吗?
- c - 关于字符串和日期大小的查询
- python - 无法使用 dj_database_url 引用 Django 数据库
- javascript - 我无法在 react native datetimepicker 上显示占位符。我使用 firebase 作为数据库
- laravel - Laravel 队列事件在页面完全加载之前立即执行
- .net - 对解决方案的任何更改都会产生 CefSharp“找不到文件”错误
- node.js - mongoose suppressWarning 选项