Need Help Adjusting A Multi-bound Tkinter Combobox Python Script To A Nested Dictionary Source
I am working on a three-level bounded tkinter combobox and I am trying to alter my script to source from a nested dictionary rather then a dictionary and a nested list. My question
Solution 1:
Here use this:
import pandas as pd
import tkinter as tk
from tkinter import ttk
df_GeoHier = {'STATE': ['AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AK','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL','AL'], 'CBSA_NAME': ['Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Anchorage, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Fairbanks, AK', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL', 'Albertville, AL'], 'ZIP_CODE': ['99501','99502','99503','99504','99505','99506','99507','99508','99509','99510','99511','99513','99514','99515','99516','99517','99518','99519','99520','99521','99522','99523','99524','99529','99530','99540','99567','99577','99587','99599','99629','99645','99652','99654','99667','99674','99676','99683','99687','99688','99694','99695','99701','99702','99703','99705','99706','99707','99708','99709','99710','99711','99712','99714','99716','99725','99775','99790','35016','35031','35087','35175','35747','35754','35755','35760','35769','35776','35950','35951','35952','35956','35957','35962','35964','35975','35976','35980']}
df = pd.DataFrame(data=df_GeoHier)
def change_dropdown(*args):
if args[0] == 'PY_VAR0':
for x, y in GeoHierDict.items():
if tkvar.get() == x:
tkvar2.set(list(y.keys())[0])
popupMenu2.configure(values=list(y.keys()))
if args[0] == 'PY_VAR1':
for x, y in GeoHierDict.items():
for key, value in y.items():
if tkvar2.get() == key:
tkvar3.set(value[0])
popupMenu3.configure(values=value)
return
root = tk.Tk()
root.geometry('250x100')
font = ("Courier", 16, "bold")
tkvar = tk.StringVar(root)
tkvar.trace('w', change_dropdown)
tkvar2 = tk.StringVar(root)
tkvar2.trace('w', change_dropdown)
GeoHierDict = {k: f.groupby('CBSA_NAME')['ZIP_CODE'].apply(list).to_dict()
for k, f in df.groupby('STATE')}
labelTop = tk.Label(root, text = "Geography",font='Helvetica 10 bold')
labelTop.grid(row=0, column=0, columnspan=2, sticky="ew")
labelTop = tk.Label(root, text = "Select a State:")
labelTop.grid(row=1, column=0, sticky=tk.W)
popupMenu1 = ttk.Combobox(root, textvariable=tkvar, values=list(set(df['STATE'])))
popupMenu1.grid(row=1, column=1)
labelTop = tk.Label(root, text = "Select a CBSA:")
labelTop.grid(row=2, column=0, sticky=tk.W)
popupMenu2 = ttk.Combobox(root, textvariable=tkvar2, values=[])
popupMenu2.grid(row=2, column=1)
tkvar3 = tk.StringVar(root)
labelTop = tk.Label(root, text = "Select a Zip Code:")
labelTop.grid(row=3, column=0, sticky=tk.W)
popupMenu3 = ttk.Combobox(root, textvariable=tkvar3, values=[])
popupMenu3.grid(row=3, column=1)
root.mainloop()
Post a Comment for "Need Help Adjusting A Multi-bound Tkinter Combobox Python Script To A Nested Dictionary Source"