将字典的字典中的项目添加到列表框,Tkinter,Python

Add items from a dictionary of a dictionary to Listbox, Tkinter, Python

我正在使用 Python tkinter 制作一个三重列表框小部件。

首先我有一个这样的字典:

testDict = {'animal': {'mammal': ['mouse', 'cat'], 'reptile': ['lizard', 'snake']}, 'food': {'meat': ['bacon', 'lamb', 'beef'], 'fruit': ['apple', 'pear', 'banana', 'kiwi']}}

现在我希望能够: 第 1 步。通过单击 "Add Theme" 按钮将 'animal' 和 'food' 作为主题添加到第一个列表框。

第二步。 Select第一个Listbox中的一个键,然后点击"Add Type"将相应的类型添加到第二个Listbox中,例如'fruit'和'meat'或'mammal'和'reptile'.

第三步。 Select a 从第二个列表框中键入并将相应的项目添加到第三个列表框中,例如:'lizard'、'snake' 或 'apple'、'pear'、'banana'、'kiwi'等

我卡在了第 3 步。

我的代码:

import tkinter as tk

from tkinter import *

from tkinter import ttk

root = Tk()

testDict = {'animal': {'mammal': ['mouse', 'cat'], 'reptile': ['lizard', 'snake']}, 'food': {'meat': ['bacon', 'lamb', 'beef'], 'fruit': ['apple', 'pear', 'banana', 'kiwi']}}

Box1 = tk.Listbox(height=8, width=50, borderwidth=2)
Box1.pack()
Box2 = tk.Listbox(height=8, width=50, borderwidth=2)
Box2.pack()
Box3 = tk.Listbox(height=8, width=50, borderwidth=2)
Box3.pack()


def addTheme():
    Box1.delete(0, END) 
    for themeName in testDict:
            Box1.insert(END, themeName)

def addType():
    getKey = str((Box1.get(ACTIVE)))
    Box1.get(ANCHOR)
    Box2.delete(0, END) 
    for itemType in testDict[getKey]:            
        Box2.insert(END, itemType)

#---------------------This is where the problem is ----------------------

def addName():
    getKey2 = str((Box2.get(ACTIVE)))
    Box2.get(ANCHOR)
    Box3.delete(0, END)
    for itemName2 in testDict[getKey2]:
        Box3.insert(END, itemName2)


themeButton = tk.Button( text = "Add Theme", command= lambda: addTheme())
themeButton.pack(side=TOP)

typeButton = tk.Button(text = "Add Type", command= lambda: addType())
typeButton.pack(side=TOP)

nameButton = tk.Button(text = "Add Name", command= lambda: addName())
nameButton.pack(side=TOP)


root.mainloop()

你能帮我修复 addName 按钮使其正常工作吗? 任何帮助将不胜感激。

提前致谢!

stovfl 的建议非常有效。谢谢大家的意见。

我把更正后的代码放在这里:

import tkinter as tk

from tkinter import *

root = Tk()

testDict = {'animal': {'mammal': ['mouse', 'cat'], 'reptile': ['lizard', 'snake']}, 'food': {'meat': ['bacon', 'lamb', 'beef'], 'fruit': ['apple', 'pear', 'banana', 'kiwi']}}

Box1 = tk.Listbox(height=8, width=50, borderwidth=2)
Box1.pack()
Box2 = tk.Listbox(height=8, width=50, borderwidth=2)
Box2.pack()
Box3 = tk.Listbox(height=8, width=50, borderwidth=2)
Box3.pack()


def addTheme():
    Box1.delete(0, END) 
    for themeName in testDict:
            Box1.insert(END, themeName)

def addType():
    getKey1 = str((Box1.get(ACTIVE)))
    Box1.get(ANCHOR)
    Box2.delete(0, END) 
    for itemType in testDict[getKey1]:            
        Box2.insert(END, itemType)



def addName():

    getKey1_for_Box2 = str((Box1.get(ACTIVE)))
    getKey2 = str((Box2.get(ACTIVE)))
    Box2.get(ANCHOR)
    Box3.delete(0, END)
    for itemName2 in testDict[getKey1_for_Box2][getKey2]:
        Box3.insert(END, itemName2)



themeButton = tk.Button( text = "Add Theme", command= lambda: addTheme())
themeButton.pack(side=TOP)

typeButton = tk.Button(text = "Add Type", command= lambda: addType())
typeButton.pack(side=TOP)

nameButton = tk.Button(text = "Add Name", command= lambda: addName())
nameButton.pack(side=TOP)


root.mainloop()