0

I am working on a simple project called restaurant management. I am trying to put the values from the entry into a dictionary. here is the code:

from tkinter import *
root=Tk()
entries = []

for i in range(3):
    en = Entry(root)
    en.grid(row=i+1, column=0)
    entries.append(en.get())

case_list = {}
for entry in entries:
    case = {'key1': entry, 'key2': entry, 'key3':entry }
    case_list.update(case)

def hallo():
    print(case_list)
button=Button(root,text="krijg",command=hallo).grid(row=12,column=0)

root.mainloop()

but when I print the dictionary to see the values for each keys, it gives me this output

{'key1': '', 'key2': '', 'key3': ''}
Ron3545
  • 13
  • 3
  • Look at a few `tkinter` tutorials and look at event driven programming. Right now you aren't giving the user any time to input the data before you call `.get()`. Also your second for loop is messed up – TheLizzard Aug 15 '21 at 07:39

2 Answers2

0

What you doing wrong is, you put in your list whats in the entry right after you created them, which is an empty string.

entries.append(en.get())

what you should do is to save a reference of that object to acces it later like:

entries.append(en)

Here is how I would do it:

import tkinter as tk
root=tk.Tk()
content = ['Pizza','Pasta','Salad','Cola']
entries = []
data_dic= {}

for idex,i in enumerate(content):
    en = tk.Entry(root)
    en.grid(row=idex+1,column=0)
    entries.append(en)
for i,j in zip(content,entries):
    data_dic.update({i : j})

def get_all():
    for k,v in data_dic.items():
        print(k, v.get())
button=tk.Button(root,text="krijg",command=get_all)
button.grid(row=12,column=0)

root.mainloop()

Relevant links:

Matiiss
  • 5,970
  • 2
  • 12
  • 29
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • btw you can format code by placing \`\`\`language (where language is python, java, etc) at the top and \`\`\` at the bottom of code blocks (as you can see in my edit) – Matiiss Aug 15 '21 at 08:12
  • @Matiiss didn't know that. Does this have any benefits? – Thingamabobs Aug 15 '21 at 08:16
  • one thing is that it is easier to edit the code here, one less indentation, also you can specify the language (for syntax highlighting), but I remember someone told me quite a while ago sth about this: [original reply](https://stackoverflow.com/a/64590921/14531062) don't really know about any other benefits, easier to paste code I guess, just prepare the backticks and copy-paste the code in the middle – Matiiss Aug 15 '21 at 08:23
  • @Matiiss thanks for let me know that. Will discover it in the future. – Thingamabobs Aug 15 '21 at 08:27
0

The first thing was to implement import tkinter as tk and make necessary changes.

I've tried to keep your code intact so I've re-arranged your code a little.

The main problem was using .get() in the wrong place.

Function hallo should have extracted data from entries list.


import tkinter as tk

root = tk.Tk()

case_list = {}
entries = []

for i in range(3):
    en = tk.Entry(root)
    en.grid(row = i, column = 0)
    entries.append(en)

def hallo():
    for i in range(3):
        case_list[f"key{i}"] = entries[ i ].get()
    print(case_list)

tk.Button(
    root, text = "Complete",
    command = hallo).grid(row = 3, column = 0)

root.mainloop()

Derek
  • 1,916
  • 2
  • 5
  • 15
  • In addition it's good practice to separate the constructor from the geometry method. Otherwise `button` becomes `None`. And why do you destroy the application after the command? – Thingamabobs Aug 15 '21 at 08:21
  • @Atlas435 I decided to destroy the widget after extracting the data in order to demonstrate the result. Normally I wouldn't. – Derek Aug 15 '21 at 08:28
  • @Derek can't you just put `print` instead of `destroy`, that way you don't need to destroy the application? – Matiiss Aug 15 '21 at 08:29
  • Yes you've convinced me, I'll make the change. – Derek Aug 15 '21 at 08:34