0

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.

1
  • price is redefined as IntVar() and is no longer a lambda. And price is never updated in your code. Commented Jun 13, 2019 at 0:42

1 Answer 1

1

You don't need lambda here (in general lambda should be extremely rare). You only need a single function to get all the data, do the calculation, and update the Label. Like this:

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

Also, it's very important to know that if you do name = Widget().place() then the name is set to None and is useless. You need to do

name = Widget()
name.grid() # or pack() or place()

or

Widget().grid() # or pack() or place()

Don't mix those 2 styles! The 2-line style is much better, and what we usually use.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, lots to learn as i'm just a newbiew, my small app is now working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.