Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Tkinter中的列表创建数据输入屏幕_Python_Dictionary_Tkinter - Fatal编程技术网

Python 从Tkinter中的列表创建数据输入屏幕

Python 从Tkinter中的列表创建数据输入屏幕,python,dictionary,tkinter,Python,Dictionary,Tkinter,我试图找出如何从列表中生成数据输入屏幕 我在这里创建了我想要的基本功能: from tkinter import * root = Tk() root.title("example") root.geometry("1200x900") def clicked(): data = (foo_entry.get(), bar_entry.get()) success_label = Label(root, text =

我试图找出如何从列表中生成数据输入屏幕

我在这里创建了我想要的基本功能:

from tkinter import *

root = Tk()
root.title("example")
root.geometry("1200x900")

def clicked():
    data = (foo_entry.get(), 
        bar_entry.get())
    success_label = Label(root, text = "Entries Stored!").grid(row = 3, column = 2)
    values_stored_label = Label(root, text = str(data)).grid(row = 4, column = 2)

foo_label= Label(root ,text = "foo").grid(row = 0,column = 0)
bar_label= Label(root ,text = "bar").grid(row = 1,column = 0)

foo_entry = Entry(root) 
foo_entry.grid(row =  0, column = 1) 
bar_entry = Entry(root) 
bar_entry.grid(row =  1, column = 1) 
submit_button = Button(root,text = "Submit Entries", command = lambda: clicked())
submit_button.grid(row = 2, column = 2)



root.mainloop()
我现在要做的是创建一个脚本,当它提供一个名称列表时,它会创建相同的数据输入屏幕。因此:

entries = ['foo','bar']
我基本上想从前面的示例中生成相同的屏幕,它创建窗口、标签和网格,但检索输入数据会在
条目中抛出一个关键错误。get()
。 代码如下:

entries = ['foo','bar']
from tkinter import *

root = Tk()
root.title("example")
root.geometry("1200x900")

def clicked():
    readout= ''
    for i in range(len(entries)):
        entryget = {entries[i]: entry[entries[i]].get()}
        readout += entryget[entries[i]]
    success_label = Label(root, text = "Entries Stored!")
    success_label.grid(row = len(entries)+2,column = 1)
    submit_label = Label(root, text = readout)
    submit_label.grid(row = len(entries)+3,column = 1)


for i in range(len(entries)):
    label = {entries[i]: Label(root ,text = entries[i])}
    labelgrid = {entries[i]:label[entries[i]].grid(row = i,column = 0)}
    entry = {entries[i]: Entry(root)}
    entrygrid = {entries[i]: entry[entries[i]].grid(row = i, column = 1)}
 
submit_button = Button(root,text = "Submit Entries", command = lambda: clicked())
submit_button.grid(row = len(entries)+1, column = 1)
root.mainloop()
弹出的错误是:

 File "examp2.py", line 11, in clicked
    entryget = {entries[i]: entry[entries[i]].get()}
KeyError: 'foo'
任何帮助都会很棒。如果有更有效的方法,请告诉我。
提前感谢

结束。您缺少的部分是,您需要创建字典或列表来存储循环之前的条目,而不是循环中的条目。试试这个:

from tkinter import *

def clicked():
    results = [entry.get() for entry in entries.values()]
    readout= ', '.join(results)
    success_label = Label(root, text = "Entries Stored!")
    success_label.grid(column = 1) # row defaults to the next available one
    submit_label = Label(root, text = readout)
    submit_label.grid(column = 1)

LABELS = ['foo','bar']
entries = {} # initialize an empty dictionary to fill later

root = Tk()
root.title("example")
root.geometry("1200x900")

for i, text in enumerate(LABELS):
    label = Label(root, text = text) # no need to store the label
    label.grid(row = i,column = 0)
    entry = Entry(root)
    entry.grid(row = i, column = 1)
    entries[text] = entry # store this entry in the global dictionary

submit_button = Button(root,text = "Submit Entries", command = clicked) # don't need lambda 
submit_button.grid(column = 1)
root.mainloop()

谢谢。这很好地解决了我的问题