如何在 python 中制作多个(超过 2 个)依赖下拉菜单
How to make multiple (more than 2) dependent Dropdowns in python
我有这个案例。有 3 个下拉列表 A、B、C。列表 A 是独立的。列表 B 取决于列表 A 中的值 select。列表 C 取决于列表 B 中的值 selected。是否可以在 Python/ Tkinter 中制作。我已经成功地完成了第一部分,即列表 B 变得依赖于列表 A 值。但是当我使用列表 B 的 selected 值生成列表 C 的值时,列表 C 显示空白值。
请帮忙!这是我的代码。
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.event_generate("<<ComboboxSelected>>")
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()
正如您在评论中所说的那样,您的代码正在运行并且列表 C 的内容确实发生了变化。
如果你想自动选择 C 的第一个选择,那么你可能正在寻找应该在 current(0):
设置之后立即出现的 event_generate 方法
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)
modclass.event_generate("<<ComboboxSelected>>")
我有这个案例。有 3 个下拉列表 A、B、C。列表 A 是独立的。列表 B 取决于列表 A 中的值 select。列表 C 取决于列表 B 中的值 selected。是否可以在 Python/ Tkinter 中制作。我已经成功地完成了第一部分,即列表 B 变得依赖于列表 A 值。但是当我使用列表 B 的 selected 值生成列表 C 的值时,列表 C 显示空白值。 请帮忙!这是我的代码。
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.event_generate("<<ComboboxSelected>>")
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()
正如您在评论中所说的那样,您的代码正在运行并且列表 C 的内容确实发生了变化。 如果你想自动选择 C 的第一个选择,那么你可能正在寻找应该在 current(0):
设置之后立即出现的 event_generate 方法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)
modclass.event_generate("<<ComboboxSelected>>")