1

所以,我的项目有收据部分,它将打印出我页面上的所有食物清单。但我只想让这个只打印复选框的数量。这是我的代码。

def Receipt():

        txtReceipt.insert(END,' \t\t *FAST FOOD CAFE* \n\n')
        txtReceipt.insert(END,'Date: '+ DateofOrder.get() +'\n')
        txtReceipt.insert(END,'Items\t\t\t\t'+"Cost of Items \n")
        txtReceipt.insert(END,'Icecream:\t\t\t\t\t' + E_Icecream.get() +'\n')
        txtReceipt.insert(END,'Donut:\t\t\t\t\t'+ E_Donut.get()+'\n')
        txtReceipt.insert(END,'Softdrink:\t\t\t\t\t'+ E_Softdrink.get()+'\n')
        txtReceipt.insert(END,'HotDog:\t\t\t\t\t'+ E_HotDog.get()+'\n')
        txtReceipt.insert(END,'Pizza:\t\t\t\t\t'+ E_Pizza.get()+'\n')
        txtReceipt.insert(END,'Taco:\t\t\t\t\t'+ E_Taco.get()+'\n')
        txtReceipt.insert(END,'Hamburger:\t\t\t\t\t'+ E_Hamburger.get()+'\n')
        txtReceipt.insert(END,'Fries:\t\t\t\t\t'+ E_Fries.get()+'\n')
        txtReceipt.insert(END,'Wings:\t\t\t\t\t'+ E_Wings.get()+'\n')
        txtReceipt.insert(END,'Cost of Drinks:\t\t\t\t'+ CostofDrinks.get()+'\nCost of Foods:\t\t\t\t'+ CostofFood.get()+"\n")
        txtReceipt.insert(END,'Delivery Charge:\t\t\t\t'+ ServiceCharge.get()+'\nTotal Cost:\t\t\t\t'+str(TotalCost.get())+"\n")
        txtReceipt.insert(END,'\nSpecial Request:   '+ Request.get())
4

1 回答 1

0

可以说,最好的解决方案是将检查按钮和条目小部件或变量放在字典或列表中,然后迭代这些值。

例如,您可以像这样创建检查按钮:

items = ("Icecream", "Donut", "Softdrink", "HotDog")
vars = {}
entries = {}
for row, item in enumerate(items):
    var = tk.BooleanVar(value=False)
    cb = tk.Checkbutton(root, text=f"{item}:", variable=var, onvalue=True, offvalue=False)
    entry = tk.Entry(root)
    vars[item] = var
    entries[item] = entry

    cb.grid(row=row, column=0, sticky="w")
    entry.grid(row=row, column=1, sticky="ew")

初始化您的varsentries字典后,要打印收据,您可以简单地遍历它们以获取选中的项目:

def receipt():
    for item in items:
        selected = vars[item].get()
        if selected:
            txtReceipt.insert("end", f"{item}: {entries[item].get()}\n")
于 2019-11-26T16:27:47.473 回答