The source code is as follows.
import tkinter
from tkinter import ttk
colors = ['Red', 'Green', 'Blue']
subcolors = ["Black", "White"]
def add_combobox_subcolors():
def inner(self):
cb_subcolors.config(values=subcolors)
cb_subcolors.set(subcolors[0])
cb_subcolors.pack()
return inner
root = tkinter.Tk()
root.geometry("200x200")
root.title("Color Picker")
cb_subcolors = ttk.Combobox(root, values=colors, width=10, state='readonly')
cb_colors = ttk.Combobox(root, values=colors, width=10, state='readonly')
cb_colors.set(colors[0])
cb_colors.bind('<<ComboboxSelected>>', add_combobox_subcolors())
cb_colors.pack()
root.mainloop()
If you change the color combo box selection, a sub-color combo box will appear. Since the list of the sub color combo box is also assigned when the color combo box is selected, This is useful when you want to change the contents of the combo box to be added depending on the element selected in the first combo box.
Thank you for reading until the end. Let's meet again.
Recommended Posts