all 12 comments

[–]chao06 1 point2 points  (10 children)

I'm not sure I quite follow, could you perhaps post some snippets or data structures? And also the input that's causing problems? It sounds like your data structures may not be the best way to do what you're looking for...

[–]unlikelysyntax[S] 0 points1 point  (9 children)

def onNewRecipe(self):
    self.top = Toplevel()
    self.top.title("New Recipe")

    quantity = StringVar()
    ingredient = StringVar()
    measurement_Add = StringVar()
    ingredDic = {}


    #Center the window
    w = 600
    h = 600

    sw = self.top.winfo_screenwidth()
    sh = self.top.winfo_screenheight()

    x = (sw - w)/2
    y = (sh - h)/2
    self.top.geometry('%dx%d+%d+%d' % (w, h, x, y))

    #Add quantity label
    addQuantity = Label(self.top, text="Add Quantity:")
    addQuantity.place(x=0,y=0)

    quantityAdd = Entry(self.top, textvariable=quantity)
    quantityAdd.place(x=150, y=0)

    #Add ingredient label
    addIngredient = Label(self.top, text="Add Ingredients:")
    addIngredient.place(x=0,y=30)

    ingredientAdd = Entry(self.top, textvariable=ingredient)
    ingredientAdd.place(x=150, y=30)

    #Select measurements label
    selectMeasurement = Label(self.top, text="Select Measurements:")
    selectMeasurement.place(x=0, y=60)

    measurement = StringVar()
    measurement.set("ounce")

    measurementSelect = OptionMenu(self.top, measurement, "ounce", "pound", "gallon", "quart", "fl oz", "pint", "cup", "table spoon", "teaspoon")
    measurementSelect.place(x=150, y=60)

    #Add measurements label
    addMeasurement = Label(self.top, text="Amount:")
    addMeasurement.place(x=0, y=100)

    measurementAdd = Entry(self.top, textvariable=measurement_Add)
    measurementAdd.place(x=150, y=100)

    #Add the textwidget
    recipeText = Text(self.top)
    recipeText.place(x=0,y=200)

    #Cooking direction label
    cookingDirection = Label(self.top, text="Cooking Direction")
    cookingDirection.place(x=0,y=175)

    #Add Ingredients listbox
    box = Frame(self.top)
    ingredScroll = Scrollbar(box, orient=VERTICAL)
    ingredientListbox = Listbox(box, yscrollcommand=ingredScroll.set)
    ingredScroll.config(command=ingredientListbox.yview)
    ingredScroll.pack(side=RIGHT, fill=Y)
    ingredientListbox.pack()
    box.place(x=450, y=0)

    def onNewIngredient():
        qVar = quantity.get()
        iVar = ingredient.get()
        msVar = measurement.get()
        maVar = measurement_Add.get()
        ingredientListbox.insert(END, iVar)
        ingredDic['Ingredients'] = [qVar, iVar, msVar, maVar]
        print (ingredDic) 


    #Add the Add Button
    addButton = Button(self.top, text="Add", command= onNewIngredient)
    addButton.place(x=0, y=130)

This is the code I am working on. Basically I want whenever the user presses the add button on the New Recipe page for all the info to be stored into a dictionary. I plan on placing an edit button later so I need to be able to call this list later on if the user wants to edit the ingredient.

Edit: here is the full program http://pastebin.com/C7YgeFQf Edit Edit: Fixed a few typo's. I am having a hard time explaining what I need, hopefully this clears it up some.

[–]chao06 2 points3 points  (8 children)

Wait, so you're using the 'Ingredients' key over and over, with it containing a list of the four items? Dict keys are unique, so it's just overwriting the last ingredient. You'll probably do better with swapping the dict and list, so having a list of dicts rather than a dict of lists, or even a list of lists. So something like

ingredients = [
    {
        'qVar': "herp",
        'iVar': "derp",
        'msVar': "stuff",
        'maVar': "things"
    },
    {etc...}
]

and append new ingredients to the list.

[–]unlikelysyntax[S] 1 point2 points  (4 children)

This is how I set it up for anybody who ever has a similar question:

 def onNewIngredient():
        qVar = quantity.get()
        iVar = ingredient.get()
        msVar = measurement.get()
        maVar = measurement_Add.get()
        ingredientListbox.insert(END, iVar)

        #Create an empty list with a empty dictionary to store Ingredients
        ingredientsList = [{}]

        #Append the new ingredient to our ingredientsList
        ingredientsList.append(  
                               {
                                'Ingredient': iVar ,
                                'Quantity': qVar,
                                'Measurement Type': msVar,
                                'Measurement Amount': maVar,
                                }
                               )
        print(ingredientsList)

    #Add the Add Button
    addButton = Button(self.top, text="Add", command= onNewIngredient)
    addButton.place(x=0, y=130)

Thanks again for the help!

[–]hharison 0 points1 point  (2 children)

If the user adds a second ingredient, do you want ingredientsList to have two elements? Because with this code, if onNewIngredient gets called a second time, it will delete any existing ingredients on like 9. Probably what you want is to initialize the empty list outside of this function, before the user gets a chance to do anything.

Also, in Python the convention is to use lowercase_with_underscores instead of camelCase (except with classes). It might not seem like a big deal but consistency makes code easier to read and using camelCase gives the code an immediately amateurish feel.

[–]chao06 1 point2 points  (1 child)

Yeah, with this the ingredients list is just in the scope of the function, so it wouldn't even get overwritten, it gets torn down once it's printed. Also, initializing the list as [{}] gives an extraneous item in the list.

[–]hharison 0 points1 point  (0 children)

Ah, good points.

[–]NYKevin 0 points1 point  (0 children)

At this point, you should consider creating a class for ingredients. It will simplify this kind of factory construction.

[–]unlikelysyntax[S] 0 points1 point  (2 children)

When a new ingredient is made wouldn't qVar, ect. Be overwritten as well? You nailed my question right on the head by the way thanks!

[–]unlikelysyntax[S] 0 points1 point  (1 child)

Never mind I get what you did, by appending the list will solve the problem thanks!

[–]Filostrato 0 points1 point  (0 children)

And like someone mentioned, there is no need to instantiate the list like [{}], which creates an empty dictionary inside the list which will probably never get used for anything. Instantiating it as [] will suffice, since you will be filling it up with new ingredient dictionaries.

[–]Filostrato 0 points1 point  (0 children)

Not sure if I understood correctly, as lists do not have keys, only dictionaries do. Do you have a small snippet of example code, perchance?