我创建了一个创建和打包一些下拉菜单的功能。我现在面临的唯一问题是如何将所有6个菜单中的选择存储到列表中?我能够通过func()方法访问菜单选项,但我坚持如何存储所有这些?理想情况下,id喜欢将所有6个选项存储在列表中,这可能吗?
def func(value):
x = value
print(value)
def GetEntry(x):
var = StringVar()
var.set('select one')
formatted = x.get().split()##gets the entry and forms a list
getMeal = CsvLoadSearch(u, formatted)
meal = OptionMenu(root, var, *getMeal, command = func ).pack()
mealOneButton = Button(root, text = "query database", command = lambda : GetEntry(mealOneString))
mealTwoButton = Button(root, text = "query database", command = lambda : GetEntry(mealTwoString))
mealThreeButton = Button(root, text = "query database", command = lambda : GetEntry(mealThreeString))
mealFourButton = Button(root, text = "query database", command = lambda : GetEntry(mealFourString))
mealFiveButton = Button(root, text = "query database", command = lambda : GetEntry(mealFiveString))
mealSixButton = Button(root, text = "query database", command = lambda : GetEntry(mealSixString))
这是下面的节目:
from tkinter import *
from tkinter import ttk
import csv
import os
u = "C:\\Users\\luke daniels\\Documents\\fooddata.csv"
"""
Loads csv and compares elements from foodList with current row.
returns a list of rows that match elements in foodList.
"""
def CsvLoadSearch(u, foodList):
results = []
inputfile = open(u)
for row in csv.reader(inputfile):
for food in foodList:
if food in row[0]:
results.append(row)
##print(row)
return results
##gets selection from drop down
def func(value):
x = value
def GetEntry(x):
var = StringVar()
var.set('select one')
formatted = x.get().split()##gets the entry and forms a list
getMeal = CsvLoadSearch(u, formatted)
meal = OptionMenu(root, var, *getMeal, command = func ).pack()
root = Tk()
root.title("MacroCalc")
caloriesAim = StringVar()
protien = StringVar()
fat = StringVar()
carbs = StringVar()
mealOneString = StringVar()
mealTwoString = StringVar()
mealThreeString = StringVar()
mealFourString = StringVar()
mealFiveString = StringVar()
mealSixString = StringVar()
mealOneKeyword = Entry(root, textvariable = mealOneString)
mealTwoKeyword = Entry(root, textvariable = mealTwoString)
mealThreeKeyword = Entry(root, textvariable = mealThreeString)
mealFourKeyword = Entry(root, textvariable = mealFourString)
mealFiveKeyword = Entry(root, textvariable = mealFiveString)
mealSixKeyword = Entry(root, textvariable = mealSixString)
mealLabel = Label(root,text = "meals")
mealLabel.config(font=("Courier", 30))
mealLabel.pack()
mealLone = Label(root,text = "meal one")
mealLtwo = Label(root,text = "meal two")
mealLthree = Label(root,text = "meal three")
mealLfour = Label(root,text = "meal four")
mealLfive = Label(root,text = "meal five")
mealLsix = Label(root,text = "meal six")
caloriesLabel = Label(root,text = "calories needed").pack()
calories = Text(root, height = 1, width = 10).pack()
protienLabel= Label(root,text = "protien ratio").pack()
protien = Text(root, height = 1, width = 4).pack()
carbsLabel = Label(root,text = "carbohydrate ratio").pack()
carbs = Text(root, height = 1, width = 4).pack()
fatsLabel = Label(root,text = "fats ratio").pack()
fats = Text(root, height = 1, width = 4).pack()
displayText = Text(root).pack(side = RIGHT)
mealOneButton = Button(root, text = "query database", command = lambda : GetEntry(mealOneString))
mealTwoButton = Button(root, text = "query database", command = lambda : GetEntry(mealTwoString))
mealThreeButton = Button(root, text = "query database", command = lambda : GetEntry(mealThreeString))
mealFourButton = Button(root, text = "query database", command = lambda : GetEntry(mealFourString))
mealFiveButton = Button(root, text = "query database", command = lambda : GetEntry(mealFiveString))
mealSixButton = Button(root, text = "query database", command = lambda : GetEntry(mealSixString))
mealButtons = [mealOneButton, mealTwoButton, mealThreeButton, mealFourButton, mealFiveButton, mealSixButton]
mealKeywords = [mealOneKeyword, mealTwoKeyword, mealThreeKeyword, mealFourKeyword, mealFiveKeyword, mealSixKeyword]
mealLabels = [mealLone, mealLtwo, mealLthree, mealLfour, mealLfive, mealLsix]
##packs the drop downs and respective lables
i = 0
while i < len(mealLabels):
mealLabels[i].pack()
mealKeywords[i].pack()
mealButtons[i].pack()
##meal.pack()
i = i + 1
root.mainloop()