-2

ive currently been trying to build a GUI application in Tkinter that takes user input from an Entry and subsiquently populate a drop down menu. The probelm is that the OptionMenu keeps throwing:

Traceback (most recent call last):
  File "C:/Python34/food2GUIree.py", line 70, in <module>
    mealOneButton = Button(root, text = "query database", command = GetEntry(mealOneString))
  File "C:/Python34/food2GUIree.py", line 68, in GetEntry
    meal = OptionMenu(root, '', *getMeal).pack()
TypeError: __init__() missing 1 required positional argument: 'value'

when i replace getMeal in:

def GetEntry(x):
    formatted = x.get().split()##gets the entry and forms a list
    getMeal = CsvLoadSearch(u, formatted)
    meal = OptionMenu(root, '', *getMeal).pack()

with list = [1,2,3,4] or any other list it works fine. why is this?

bellow is the complete program:

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[0])
                ##print(row)
    return results       

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)

def GetEntry(x):
    formatted = x.get().split()##gets the entry and forms a list
    getMeal = CsvLoadSearch(u, formatted)
    meal = OptionMenu(root, '', *getMeal).pack()        

mealOneButton = Button(root, text = "query database", command = GetEntry(mealOneString)) 
mealTwoButton = Button(root, text = "query database", command = GetEntry(mealTwoString))
mealThreeButton = Button(root, text = "query database", command = GetEntry(mealThreeString))
mealFourButton = Button(root, text = "query database", command = GetEntry(mealFourString))
mealFiveButton = Button(root, text = "query database", command = GetEntry(mealFiveString))
mealSixButton = Button(root, text = "query database", command = GetEntry(mealSixString))    

mealButtons = [mealOneButton, mealTwoButton, mealThreeButton, mealFourButton, mealFiveButton, mealSixButton]
mealKeywords = [mealOneKeyword, mealTwoKeyword, mealThreeKeyword, mealFourKeyword, mealFiveKeyword, mealSixKeyword]
mealLabels = [mealLone, mealLtwo, mealLthree, mealLfour, mealLfive, mealLsix]
##meals = [mealOne, mealTwo, mealThree, mealFour, mealFive, mealSix]

##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()
101ldaniels
  • 301
  • 1
  • 6
  • 15
  • Have you verified that `getMeal` contains what you're assuming it contains? The error implies that it is empty. – Bryan Oakley Jan 30 '17 at 04:29
  • Print out `getMeal`, you are getting that error because it is empty. – Steven Summers Jan 30 '17 at 04:30
  • Please consider reducing the size of the example. If the problem is with an `OptionMenu`, you can probably remove all of the other widgets and `StringVar`s, and you can hard-code the arguments that you pass to `OptionMenu`. – Bryan Oakley Jan 30 '17 at 04:31
  • @BryanOakley yeah it does which is the weird thing. At one stage it was working if i passed the results from CsvLoadSearch() into another function and then used that. even so though if A = B = C then A = C so no idea how that temporally fixed it. – 101ldaniels Jan 30 '17 at 04:33
  • I'm not asking about "one stage". I'm asking about this specific example that is failing. Have you taken the failing code and printed out what is in `getMeal`. I can almost guarantee it's an empty string or empty list. – Bryan Oakley Jan 30 '17 at 04:35
  • @BryanOakley yes to begin with its empty. how to i overcome this before the user has had a chance to enter something? – 101ldaniels Jan 30 '17 at 04:38

1 Answers1

-1

throw away lambda function is needed after command when creating the buttons

101ldaniels
  • 301
  • 1
  • 6
  • 15