如何使用 spinbox 选择列表中的项目,然后使用添加到购物车按钮将该项目添加到发票? Python 3

编程入门 行业动态 更新时间:2024-10-27 00:23:04
本文介绍了如何使用 spinbox 选择列表中的项目,然后使用添加到购物车按钮将该项目添加到发票? Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
    from tkinter import *  

这是两个包含价格和产品详细信息的列表.

    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):  
    '\n'  
    var_1=('#'+description_audible[m])+ '\n' +('('+price_audible[m]+')')+'\n'  
    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()

当您运行此 python 代码时,它会创建一个窗口,其中包含一个单选按钮,当单击该单选按钮时,它会弹出另一个窗口,其中显示了从 1 到 10 个项目的商品价格及其名称.有一个数值从 1 到 10 的旋转框.还有一个叫做添加到购物车的按钮.如何将此旋转框连接到其余代码和添加到购物车按钮.因此,当有人想购买第 9 件商品时,他们将使用旋转框选择第 9 件商品,然后当他们按下添加到购物车按钮时,该商品将被添加到列表中.这将显示项目的总成本.

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.

推荐答案

因此,当您单击添加到购物车"按钮时,程序应首先获取 Spinbox 的值,然后使用该数据从一个列表并将其价格添加到另一个变量中.

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.

您可以使用 spin_box.get() 从 Spinbox 获取值.要在单击时运行函数,请将属性 command=your_function 添加到 Button(dash_1, text = 'Add to cart').pack().然后就可以添加函数了:

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]

注意:您必须将 price_audible 中的数据更改为数字,而不是字符串.您可以使用字符串插值在显示到屏幕时重新添加美元符号.

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.

这篇关于如何使用 spinbox 选择列表中的项目,然后使用添加到购物车按钮将该项目添加到发票? Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 05:40:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   购物车   如何使用   将该   发票

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!