these two functions are not working as args in the lamba function which is calculating the price of the sweets
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))
I have tried nested functions but they are not working, help please anyone
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()
the code works except that price is not being displayed as the lambda function is not working.
这里不需要lambda(一般来说lambda应该非常罕见)。您只需要一个函数就可以获取所有数据、进行计算和更新标签。像这样:
另外,非常重要的是要知道,如果您执行
name = Widget().place()
,那么名称被设置为None
,并且是无用的。你需要做什么或者
别把这两种风格混在一起!2行样式更好,我们通常使用的是。你知道吗
相关问题 更多 >
编程相关推荐