0

How do I use what is entered into a Tkinter entry widget in the rest of the program? I have this for the entry:

n = StringVar()
Entry(search1, textvariable=n).pack()
label = n.get()

with open('data.txt') as json_file:
    data = json.load(json_file)
    for p in data:
        recipe = p['recipe']
        recipeNames= recipe['label']
        if recipeNames == label:
            recipeName = label
            ingredients = []
            for j in recipe['ingredients']:
                ingredients.append(j['text'])
Button(search1, text="Enter", command=partial(addrecipe, recipeName, calendarID, ingredients, service)).pack()

When I try to use the variable it has nothing in it and comes back with an error. How do I assign a variable to what is in the entry widget?

Riley Spotton
  • 231
  • 1
  • 2
  • 8
  • Most of this code belongs in the definition of `addrecipe`, to be executed once you press the button, not immediately. – chepner Feb 23 '20 at 23:50
  • 3
    Does this answer your question? [Python Tkinter Entry get()](https://stackoverflow.com/questions/35662844/python-tkinter-entry-get) – stovfl Feb 23 '20 at 23:50
  • 1
    maybe you can use lambda: before writhing command as the command requires parameters – Hardik Jain Feb 24 '20 at 03:09
  • There might be other problems in your code but as you have not shared that I can only figure out one problem. So what you should do is use `command=` **`lambda:`** `partial(addrecipe, recipeName, calendarID, ingredients, service)).pack()`, instead of `command=partial(addrecipe, recipeName, calendarID, ingredients, service)).pack()` – DaniyalAhmadSE Feb 24 '20 at 13:54

0 Answers0