Mario Pece
Mario Pece

Reputation: 171

How to change background color in ttk.Combobox's listview?

How can I change the style of the combobox's listview?

Here is part of the code so far:

style = ttk.Style()
style.configure("BW.TLabel", foreground="black", background="#20252b",
                insertbackground="white", fieldbackground= 'blue')
optmn = ttk.Combobox(self, style="BW.TLabel")
optmn.place(x=140, y=200, width=150, height=25)

How can I access the style of the listview of the combobox?

Sample Image:

enter image description here

Upvotes: 7

Views: 6602

Answers (2)

Sean Richards
Sean Richards

Reputation: 111

Your answer to your own question helped me solve mine. So, I figured I'd help you and others by contributing a bit further :)

Seeing your code in the question leads me to believe you want to change more than just the background color of the Listbox. I've played around with this a bit, and I'm sure there are some other options available, but this allows you to update the Combobox entry field background and text colors, as well as the Listbox field background and text colors.

I've included an example picture of a Combobox/Listbox from an app I've written using the code below (adapted for this response).

Hope this helps you and any future travelers!

import tkinter as tk
from tkinter import ttk

# variables created for colors
ebg = '#404040'
fg = '#FFFFFF'

root = tk.Tk()

style = ttk.Style()

# Note the code line below.
# Be sure to include this or style.map() won't function as expected.
style.theme_use('alt')

# the following alters the Listbox
root.option_add('*TCombobox*Listbox*Background', ebg)
root.option_add('*TCombobox*Listbox*Foreground', fg)
root.option_add('*TCombobox*Listbox*selectBackground', fg)
root.option_add('*TCombobox*Listbox*selectForeground', ebg)

# the following alters the Combobox entry field
style.map('TCombobox', fieldbackground=[('readonly', ebg)])
style.map('TCombobox', selectbackground=[('readonly', ebg)])
style.map('TCombobox', selectforeground=[('readonly', fg)])
style.map('TCombobox', background=[('readonly', ebg)])
style.map('TCombobox', foreground=[('readonly', fg)])

Combobox and Listbox color example

Upvotes: 2

Mario Pece
Mario Pece

Reputation: 171

Found it! the way to change the BG of the listview of the combobox is:

import ttk
import Tkinter
root = Tkinter.Tk()

root.option_add("*TCombobox*Listbox*Background", 'green')

combo = ttk.Combobox().pack()
root.mainloop()

Upvotes: 10

Related Questions