我最近开始使用 python 进行编程,目前正在开发一个餐厅可以接受订单的程序,以进行练习。“菜单”以 CSV 文件的形式提供。我的 GUI 目前看起来像这样:
https://bilderupload.org/bild/983a84049-gui8860a4e27cbbe4c6343984
“食物”下的两个复选按钮和条目是有效的两个,也是我单独创建的两个。饮料下的复选按钮是使用 csv 文档中的内容创建的。我可以勾选复选按钮,但该条目仍然是灰色的,即使复选按钮中有一个勾号。
您必须勾选您订购的菜肴以激活输入字段(如果勾选按钮中没有勾选,您无法在该字段中输入内容)。
如果我为每道菜单独创建检查按钮和条目变量,则此方法有效,但如果我想将 CSV 文件中的内容用作变量,则不行。有人可以帮助我或有更好的想法来解决我的问题吗?
.csv 文件如下所示:
https://bilderupload.org/bild/b8c286400-food-csv
# Use CSV file
food = open('food.csv', newline='')
food_csv = csv.reader(food, delimiter=';')
food_name = [item[0] for item in food_csv]
food_left = food_name[1:10]
drinks_middle = food_name[10:]
drinks_dict = {}
drinks_dict_string = {}
count_i = 1
count_j = 1
# Function to connect Checkbutton and Entry (Food)
def costReference():
food_string_1.set("0")
food_string_2.set("0")
if (food_var1.get() == 1):
txt_food_1.configure(state=tk.NORMAL)
txt_food_1.focus()
txt_food_1.delete('0', tk.END)
food_string_1.set("")
elif (food_var1.get() == 0):
txt_food_1.configure(state=tk.DISABLED)
food_string_1.set("0")
if (food_var2.get() == 1):
txt_food_2.configure(state=tk.NORMAL)
txt_food_2.focus()
txt_food_2.delete('0', tk.END)
food_string_2.set("")
elif (food_var2.get() == 0):
txt_food_2.configure(state=tk.DISABLED)
food_string_2.set("0")
# 2 Checkbuttons and Entrys for Food
food_var1 = tk.IntVar()
food_var2 = tk.IntVar()
food_string_1 = tk.StringVar()
food_string_2 = tk.StringVar()
chck_food_1 = tk.Checkbutton(left_frame, variable=food_var1, onvalue=1,
offvalue=0, command=costReference)
chck_food_1.grid(column=0, row=1, sticky=tk.W)
chck_food_2 = tk.Checkbutton(left_frame, variable=food_var2, onvalue=1,
offvalue=0, command=costReference)
chck_food_2.grid(column=0, row=2, sticky=tk.W)
txt_food_1 = tk.Entry(left_frame, font=('arial', 15, 'normal'),
textvariable=food_string_1, bd=10, width=4,
justify='right', state=tk.DISABLED)
txt_food_1.grid(column=2, row=1)
txt_food_2 = tk.Entry(left_frame, font=('arial', 15, 'normal'),
textvariable=food_string_2, bd=10, width=4,
justify='right', state=tk.DISABLED)
txt_food_2.grid(column=2, row=2)
# Checkbutton and Entry for Drinks
for i in drinks_middle:
drinks_dict[i] = tk.IntVar()
chck_drinks = tk.Checkbutton(middle_frame, variable=drinks_dict[i],
onvalue=1, offvalue=0)
chck_drinks.grid(column=0, row=count_i, sticky=tk.W)
count_i += 1
for j in drinks_middle:
drinks_dict_string[j] = tk.StringVar()
txt_drinks = tk.Entry(middle_frame, font=('arial', 15, 'normal'),
textvariable=drinks_dict_string[j], bd=10, width=4,
justify='right', state=tk.DISABLED)
txt_drinks.grid(column=2, row=count_j)
count_j += 1