I have this case. There are 3 dropdown lists A,B,C. List A is independent. List B depends on value select in List A. List C depends on value selected in List B. Is it possible to make in Python/ Tkinter. I have successfully made the first part i-e List B becomes dependent on List A value. But when I use the selected value of List B to generate value of List C, List C shows blank values. Please help! Here is my code.
import tkinter
import customtkinter # <- import the CustomTkinter module
from tkinter import *
from tkinter import ttk
from openpyxl import *
from PIL import ImageTk, Image
# Initializations
customtkinter.set_appearance_mode("Light") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
root_tk.geometry("1030x620")
root_tk.title("CustomTkinter Test")
# region Configure Custome Style of Combobox and Entry Widgets
style= ttk.Style()
style.theme_use('clam')
style.configure("TCombobox", foreground="#1e1e1e",arrowcolor="#1e1e1e")
style.map("TCombobox",fieldbackground=[("disabled", "1e1e1e")])
style.map("TEntry",fieldbackground=[("disabled", "1e1e1e")])
# endregion
# Appearances
# Text colors for labels
nlcolor = "#1e1e1e" # -------- Normal Label Text Color
hdcolor = "#EB3F3F" # -------- Headings Background Color
# Binder Functions
def bind_mkmodel(e):
if mkmodel.get()=="3051CD":
modclass.configure(state=tkinter.NORMAL)
modclass.config(values=list_class1)
modclass.current(0)
elif mkmodel.get()=="3051SCD":
modclass.configure(state=tkinter.NORMAL)
modclass.config(values=list_class2)
modclass.current(0)
elif mkmodel.get()=="3051SMV--1or 2":
modclass.configure(state=tkinter.NORMAL)
modclass.config(values=list_class3)
modclass.current(0)
elif mkmodel.get() == "3051SMV--3 or 4":
modclass.configure(state=tkinter.NORMAL)
modclass.config(values=list_class4)
modclass.current(0)
else:
modclass.config(values=[""])
modclass.current(0)
modclass.configure(state=tkinter.DISABLED)
def bind_modclass(e):
if modclass.get()=="Std":
dp.configure(state=tkinter.NORMAL)
dp.config(values=list_dp1)
dp.current(0)
elif modclass.get()=="S_Ultra" or modclass.get() == "S_Classic":
dp.configure(state=tkinter.NORMAL)
dp.config(values=list_dp2)
dp.current(0)
elif modclass.get() == "S_Ultra for Flow":
dp.configure(state=tkinter.NORMAL)
dp.config(values=list_dp3)
dp.current(0)
elif modclass.get() == "SMV12_Classic" or modclass.get() == "SMV34_Ultra" or modclass.get() == "SMV34_Classic":
dp.configure(state=tkinter.NORMAL)
dp.config(values=list_dp4)
dp.current(0)
elif modclass.get() == "SMV12_Ultra for Flow":
dp.configure(state=tkinter.NORMAL)
dp.config(values=list_dp5)
dp.current(0)
elif modclass.get() == "SMV34_Ultra for Flow":
dp.configure(state=tkinter.NORMAL)
dp.config(values=list_dp6)
dp.current(0)
else:
dp.config(values=[""])
dp.current(0)
dp.configure(state=tkinter.DISABLED)
# region Frames definition
frame_0 = Frame(root_tk,background="white")
frame_0.place(x=0,y=0,height=1100,width=150)
frame_6 = customtkinter.CTkFrame(master=root_tk, corner_radius=15,fg_color="white")
frame_6.place(x=10,y=10)
# endregion
# --------- FRAME 6 COMPONENTS ---------
# Labels Creation
lab_heading6 = Label(frame_6, text=" Dropdowns",anchor=W,width=35,font='Arial 10 bold',bg=hdcolor,fg="white")
lab_mkmodel = Label(frame_6, text=" List A",anchor=W,width=23,bg="#2E2E2E",pady=2,background="white",fg=nlcolor)
lab_modclass = Label(frame_6, text=" List B",anchor=W,width=23,bg="#2E2E2E",pady=2,background="white",fg=nlcolor)
lab_dp = Label(frame_6, text=" List C",anchor=W,width=23,bg="#2E2E2E",pady=2,background="white",fg=nlcolor)
# Labels positioning
lab_heading6.grid(row=0, column=0,pady=10,columnspan=2)
lab_mkmodel.grid(row=1, column=0)
lab_modclass.grid(row=2, column=0)
lab_dp.grid(row=3, column=0)
# Creating List A dropdown
list_mkmodel = ["3051CD", "3051SCD", "3051SMV--1or 2","3051SMV--3 or 4","Custom"]
mkmodel = ttk.Combobox(frame_6,values=list_mkmodel,width=13,style="TCombobox")
mkmodel.grid(row=1, column=1, ipadx="11")
mkmodel.bind("<<ComboboxSelected>>",bind_mkmodel)
# Creating List B dropdown
list_class1 = ["Std"]
list_class2 = ["S_Ultra", "S_Classic","S_Ultra for Flow"]
list_class3 = ["SMV12_Classic", "SMV12_Ultra for Flow"]
list_class4 = ["SMV34_Ultra","SMV34_Classic", "SMV34_Ultra for Flow"]
modclass = ttk.Combobox(frame_6,values=[""],width=13,style="TCombobox")
modclass.grid(row=2, column=1, ipadx="11")
modclass.current(0)
modclass.bind("<<ComboboxSelected>>",bind_modclass)
# Creating List C dropdown
list_dp1 = ["1,2,3,4,5"]
list_dp2 = ["1A,2A,3A,4A,5A"]
list_dp3 = ["2A,3A"]
list_dp4 = ["1,2,2 SP=5,3,3 SP=5,4,5"]
list_dp5 = ["2,2 SP=5,3,3 SP=5,4"]
list_dp6 = ["2,2 SP=5,3,3 SP=5"]
dp = ttk.Combobox(frame_6,values=[""],width=13,style="TCombobox")
dp.current(0)
dp.grid(row=3, column=1, ipadx="11")
root_tk.mainloop()
CodePudding user response:
As you've been told in the comment, your code is working and list C content does change. If you want to automate the first selection of C then you are probably looking for the event_generate method that should come right after the setting of current(0):
modclass.current(0)
modclass.event_generate("<<ComboboxSelected>>")