如何从其他列表中的元素创建名称的新列表

时间:2017-11-22 07:30:22

标签: python-3.x list tkinter widget append

我想在其他列表中使用元素列表名称,但会出现一些错误。 如果有title_list = [A, B, C],我想列出ex B.append(test_items)

from tkinter import Tk, Button, Entry
import tkinter as tk

ent1_input = []
ent2_input = []
title_list = []
test_item_list = []
entry1 = 0
entry2 = 0

def addentry():
    ent1 = Entry(root)
    ent1.pack()
    ent1_input.append(ent1)

def click_B():
    global title

    for entry1 in ent1_input:
        title = entry1.get()
        create_window()
        title_list.append(title)

def create_window():

    def addBox2():
        ent2 = Entry(new_root)
        ent2.pack()
        ent2_input.append(ent2)

    def test_item_list_up():
        global test_items

        for i in range(0, len(title_list)):

            for entry2 in ent2_input:
                test_items = entry2.get()
                title_lsit[i].append(test_items) # <---- What do i change it

    new_root = tk.Tk()
    new_root.geometry('200x200')
    addboxButton = Button(new_root, text= title , command=addBox2)
    addboxButton.pack(side="top", expand=True, padx=1, pady=1)
    applyButton = Button(new_root, text="Apply value",command=test_item_list_up)
    applyButton.pack(side="bottom", expand=True, padx=1, pady=1)

root = Tk()
root.geometry('200x200')
root.title('initialization')
root.configure(background='green')
b = Button(root, text="Set Items", command=addentry)
b.pack(side="top", expand=True, padx=1, pady=1)
c = Button(root, text="Apply Items", command=click_B)
c.pack(side="bottom", expand=True, padx=1, pady=1)

root.mainloop()

可以查看一些示例代码,我几周​​来一直在研究 Python

1 个答案:

答案 0 :(得分:3)

你无法制作这样的清单。你可以写一本字典。

a_list = ['a', 'b', 'c']
a_dict = dict.fromkeys(a_list, list())
for k in a_dict:
    a_dict[k].append(test_items)
相关问题