How Can You Make 3 (or 4) Nested Binded Comboxes Uting Tkinter In Python?
I am trying to make a GUI that has binding dropdowns that rely off the previous dropdown. The format that I am trying to achieve is as follows: Select a {level 1} Select a {level
Solution 1:
Here is a solution which you can improve further by add zip code too.
import tkinter as tk
from tkinter import ttk
cars = {"Bugatti":["Veyron","Golf"] ,"BMW":["330M"]}
defchange_dropdown(*args):
print("Chosen brand " + tkvar.get())
for x, y in cars.items():
if tkvar.get() == x:
tkvar2.set(y[0])
popupMenu2.configure(values=y)
root = tk.Tk()
canvas = tk.Canvas(root, height=500, width= 500, bg="white")
canvas.pack()
tkvar = tk.StringVar(root)
tkvar2 = tk.StringVar(root)
tkvar3 = tk.StringVar(root)
popupMenu1 = ttk.Combobox(canvas, textvariable=tkvar, values=list(cars.keys()))
popupMenu1.pack()
popupMenu1.bind('<<comboboxselected>>', change_dropdown)
popupMenu2 = ttk.Combobox(canvas, textvariable=tkvar2, values=[])
popupMenu2.pack()
popupMenu1.bind('<<comboboxselected>>', change_dropdown)
popupMenu3 = ttk.Combobox(canvas, textvariable=tkvar3, values=[])
popupMenu3.pack()
root.mainloop()
Update:
here is the fully functioning code:
import tkinter as tk
from tkinter import ttk
cars = {"Bugatti":["Veyron","Golf"] ,"BMW":["330M"]}
_zip = [["TEST1"],["TEST2"],["TEST3"]]
lst = [x for y inlist(cars.values()) for x in y]
defchange_dropdown(*args):
print("Chosen brand " + tkvar.get())
if args[0] == 'PY_VAR0':
for x, y in cars.items():
if tkvar.get() == x:
tkvar2.set(y[0])
popupMenu2.configure(values=y)
if args[0] == 'PY_VAR1':
for x, y inzip(lst, _zip):
if tkvar2.get() == x:
tkvar3.set(y[0])
popupMenu3.configure(values=y)
return
root = tk.Tk()
canvas = tk.Canvas(root, height=500, width= 500, bg="white")
canvas.pack()
tkvar = tk.StringVar(root)
tkvar.trace('w', change_dropdown)
tkvar2 = tk.StringVar(root)
tkvar2.trace('w', change_dropdown)
tkvar3 = tk.StringVar(root)
popupMenu1 = ttk.Combobox(canvas, textvariable=tkvar, values=list(cars.keys()))
popupMenu1.pack()
popupMenu2 = ttk.Combobox(canvas, textvariable=tkvar2, values=[])
popupMenu2.pack()
popupMenu3 = ttk.Combobox(canvas, textvariable=tkvar3, values=[])
popupMenu3.pack()
root.mainloop()
Post a Comment for "How Can You Make 3 (or 4) Nested Binded Comboxes Uting Tkinter In Python?"