[PYTHON] Change the movement by changing the combo box with tkinter

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

Change the movement by changing the combo box with tkinter
Create a matrix with PythonGUI (tkinter combo box)
Make Fatjar by changing the main class with Gradle
Install by specifying the version with pip
When changing the table name with flask_migrate
Can't change aspect_ratio with sympy.plotting? About the matter
I tried playing with the calculator on tkinter
Broadcast operation by raising the dimension with np.newaxis
Change the string to be replaced according to the matched string by replacing with Python regular expression