我有這個案子。有 3 個下拉串列 A、B、C。串列 A 是獨立的。串列 B 取決于串列 A 中的值選擇。串列 C 取決于串列 B 中選擇的值。是否可以在 Python/Tkinter 中創建。我已經成功制作了第一部分,即串列 B 依賴于串列 A 的值。但是當我使用串列 B 的選定值生成串列 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.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()
uj5u.com熱心網友回復:
正如您在評論中被告知的那樣,您的代碼正在運行,并且串列 C 內容確實發生了變化。如果您想自動化 C 的第一次選擇,那么您可能正在尋找應該在 current(0) 設定之后立即出現的 event_generate 方法:
modclass.current(0)
modclass.event_generate("<<ComboboxSelected>>")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/474302.html