因此,我在项目中有收据部分,该部分将在页面上打印出所有食物清单。 但是我只希望它仅打印复选框的数量。这是我的代码。
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())
答案 0 :(得分: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")
初始化您的vars
和entries
字典后,要打印出收据,您可以简单地对其进行迭代以获取已检查的项目:
def receipt():
for item in items:
selected = vars[item].get()
if selected:
txtReceipt.insert("end", f"{item}: {entries[item].get()}\n")