How to add different background colors to different selection options in ttk.Combobox?

Question:

I want to add different colors to different selections in Combobox. I found questions about changing the overall background color, but not per entry.

I’m attaching some examples below.

enter image description here
enter image description here
This is an example from your answer @toyota-supra

Asked By: Mike Azatov

||

Answers:

The Ttk Combobox does not supports that feature. So it’s recommended to implement a custom widget with the Entry and Menu as in the ctk_combobox.py.

But if you don’t mind hacking the Tk, you can try like the following.(It may break in the future.)

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

combo = ttk.Combobox(root, values = ['red', 'green', 'blue'], state = 'readonly')
combo.pack()

def configure_items():
    lines = [f'set popdown [ttk::combobox::PopdownWindow {combo}]']
    for i, v in enumerate(combo['values']):
        lines.append(f'$popdown.f.l itemconfigure {i} -background {v}')
    root.tk.eval('n'.join(lines))
combo.configure(postcommand=lambda: root.after_idle(configure_items))

root.mainloop()

To understand the above code, you need to see the combobox.tcl and the listbox reference.

Answered By: relent95