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

2

7

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

Lilley answered 21/7, 2015 at 17:16 Comment(6)
Thanks for the edit @ajcr !Lilley
No worries - good question, I'd like to know the answer too!Emrick
In a tcl documentation (wiki.tcl.tk/37973) i found this: ttk::combobox edit ttk::style map TCombobox -background ` option add TComboboxListbox.background color` option add *TCombobox*Listbox.foreground color option add *TCombobox*Listbox.selectBackground color option add *TCombobox*Listbox.selectForeground colorLilley
This is really annoying, there's no known way to really customise ttk widgets, and people say that they are customisable, well, in my opinion, they are a waste of time.Invocation
You stlye.configure a label. It should be TComboboxStanton
Thanks Curly Joe but .option_add() did the jobLilley
L
10

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()
Lilley answered 23/7, 2015 at 7:52 Comment(0)
R
2

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

Robbyrobbyn answered 15/6, 2021 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.