How to set the background color of ttk.Combobox
I have a problem setting the background color for a Combobox using tkinter ttk with "vista" theme (I am using Python 3). I tried the code from here ttk.Combobox crashes when the state is read-only and out of focus
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
combo = ttk.Combobox(root, values=['1', '2', '3'])
combo['state'] = 'readonly'
combo.pack()
tk.Entry(root).pack()
style = ttk.Style()
style.map('TCombobox', selectbackground=[('readonly', 'red')])
#style.map('TCombobox', fieldbackground=[('readonly', 'blue')]) #not working as well
But this will only change the background for the text, the rest of the combobox will remain white. Also I saw a post on the tcl forum: http://wiki.tcl.tk/15780 and I tried using 'fieldbackground' but it seems that tkinter is ignoring this setting.Do you have any idea how to solve it? Maybe there is a way to customize only a specific style in a specific theme? I have seen that for the "default" theme, the background changes to gray if the state is "readonly".
source to share
This code below worked fine for me. It is important to establish the order of the parameters.
`
style = ttk.Style()
style.map('TCombobox', fieldbackground=[('readonly','white')])
style.map('TCombobox', selectbackground=[('readonly', 'white')])
style.map('TCombobox', selectforeground=[('readonly', 'black')])
self.mycombo = ttk.Combobox(self.frame,textvariable=self.combo_var,
height=15,justify='left',width=21,
values=lista)
self.mycombo['state'] = 'readonly' # Set the state according to configure colors
self.mycombo.bind('<<ComboboxSelected>>',
lambda event: self._click_combo())
`
source to share
Apparently, the order given for the properties of the new style is important in determining whether a particular property of the new style will be applied or not. For example, if I first set background
instead selectbackground
, then the highlight color will not change, but only the color of the mini arrow button (to display a list of options).
I also noted that depending on the value parent
, which I assume is the parent style from which the new style is derived, some of the new settings and properties of the new style may not apply. For example, if I try to change a property fieldbackground
when parent
set to aqua
, it doesn't work, but if parent
set to alt
, it works. (I hope more experienced users can help and contribute to improving this answer, which might be helpful for future users as ttk
well tkinter
).
This is my solution where I created a completely new style:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
combostyle = ttk.Style()
combostyle.theme_create('combostyle', parent='alt',
settings = {'TCombobox':
{'configure':
{'selectbackground': 'blue',
'fieldbackground': 'red',
'background': 'green'
}}}
)
# ATTENTION: this applies the new style 'combostyle' to all ttk.Combobox
combostyle.theme_use('combostyle')
# show the current styles
# print(combostyle.theme_names())
combo = ttk.Combobox(root, values=['1', '2', '3'])
combo['state'] = 'readonly'
combo.pack()
entry = tk.Entry(root)
entry.pack()
root.mainloop()
Since I am not an expert in ttk
, I was not able to apply the new theme only to a specific instance of a type ttk.Combobox
, but I applied this theme to all instances of the future possible ttk.Combobox
. If anyone can improve this answer I would really appreciate a gesture!
For more information on how to create and set new styles, see here or.
source to share