Poppel
Poppel

Reputation: 17

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

I'm making a triple listbox widget with Python tkinter.

First I have a dict of a dict like this:

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

Now I want to be able to: Step1. add 'animal' and 'food' as Theme to the first Listbox by clicking "Add Theme" button.

Step2. Select a key in the first Listbox, then click "Add Type" to add the corresponding types to the second Listbox, for instance'fruit' and 'meat' or 'mammal' and 'reptile'.

Step3. Select a Type from the second Listbox and add the corresponding items to the third Listbox, for instance: 'lizard', 'snake', or 'apple', 'pear', 'banana', 'kiwi' etc.

I got stuck on Step3.

My Code:

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()

Can you help me fix the addName button to make it work? Any help would be appreciated.

Thanks in advance!

Upvotes: 0

Views: 892

Answers (1)

Poppel
Poppel

Reputation: 17

stovfl's suggestion works like a charm. Thank you people for your input.

I put the corrected code here:

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()

Upvotes: 1

Related Questions