Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

tkinter - How to use spinbox for selection of items in a list and then using add to cart button to add that item to the invoice?Python 3

    from tkinter import *  

These are two lists which has prices and product details.

    price_audible = ['$1.99', '$1.50', '$1.00', '$1.99', '$1.50', '$1.00', '$1.99', '$1.50', '$1.00', '$1.99']  

    description_audible = ['1: Aaaaaa', '2: bbbbbb', '3: ccccccccc', '4: dddddddddd', '5:eeeeeeee',
                   '6: fffffff', '7: gggggggg', '8: hhhh', '9: ii', '10: jjjjjjjjjjjjjjjjj']  
    audible_newlist = ''  
    for m in range(0, 10):  
    '
'  
    var_1=('#'+description_audible[m])+ '
' +('('+price_audible[m]+')')+'
'  
    audible_newlist = audible_newlist + var_1  
    show_window = audible_newlist  

    dash_1 = Tk()  
    dash_1.configure(bg = 'red')
    dash_1.title("Amazon Bestsellers Audible") 

    show_window = StringVar()
    show_window.set(audible_newlist)
    conction = IntVar()

    def audible_catagory():
    new_pc = Toplevel(dash_1)
    new_pc.title('Amazon Bestsellers PC')
    Label(new_pc, text = "Amazon Bestsellers PC", font = ('Arial', 14),
    bg='red', fg='floralwhite').pack()
    Label(new_pc, textvariable = show_window, justify = LEFT, bg= 'light yellow',takefocus = True).pack()

    Label(dash_1, text = "Amazon Bestsellers Audible", font = ('Arial', 14), bg='red', fg='floralwhite').pack()
    Label(dash_1, text = show_window).pack()
    Button(dash_1, text = 'Add to cart').pack()
    Radiobutton(dash_1, text = 'Amazon Audible', value = True, variable = conction, command =audible_catagory).place(x=0, y=0)

    spin_box = Spinbox(dash_1, from_=0, to=10, fg = 'black',
        width = 2, justify = RIGHT, relief = 'flat', bg = 'red').pack()
    dash_1.mainloop()

When you run this python code it creates a window, which has a radiobutton, and when that radiobutton clicked it pops up another window which displaying the items price and its name from 1 to 10 items. There is a spinbox when which has values from 1 to 10. There is also a button called add to cart. How do I connect this spinbox to the rest of the code and the add to cart button. So when someone want to buy item number nine they will use the spinbox to select item nine and then when they press add to cart button the item will be added to a list. which will display the total cost of the items.

If anyone can help will be much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So when you click the Add to Cart button, the program should first get the value of the Spinbox, then use that data to get the price of the item from a list and add its price to another variable.

You can use spin_box.get() to get the value from the Spinbox. To run a function on click, add the attribute command=your_function to Button(dash_1, text = 'Add to cart').pack(). Then you can add the function:

def your_function():
    item_number = spinbox.get()
    audible_cart_total_price += price_audible[item_number]

NOTE: You will have to change your data in price_audible to be numbers, not strings. You can use string interpolation to add the dollar signs back when displaying to the screen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...