Search code examples
python-3.xtkintercombobox

Combobox changing out of focus background and foreground color


I need to change the foreground and background colour of combobox input field when it is not in focus. I read through these this and this but am not able to figure out which one to use.

I am using Python 3.9.6 on Windows 10.

Here is a sample code.

import tkinter as objTK
from tkinter import ttk as objTTK

objWindow = objTK.Tk()

objStyle = objTTK.Style()
objStyle.theme_use("clam")
objStyle.configure("TCombobox", backgroundcolor="orange")
objStyle.configure("TCombobox", foregroundcolor="blue")

tMonths = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
cbDtRStM = objTTK.Combobox(master=objWindow, width=10, state="readonly", values=tMonths)
cbDtRStM.pack()

btnButton = objTTK.Button(master=objWindow, text="Test")
btnButton.pack()

objWindow.bind("<Escape>", lambda funcWinSum: objWindow.destroy())

objWindow.mainloop()

Currently the text is displayed as grayed out as in picture below.

Picture

I would like to make the text as black with background as white.


Solution

  • I was able to resolve this after reading this

    Here is the updated code.

    import tkinter as objTK
    from tkinter import ttk as objTTK
    
    objWindow = objTK.Tk()
    
    objStyle = objTTK.Style()
    objStyle.theme_use("clam")
    objStyle.map("TCombobox", selectforeground=[('readonly', '!focus', 'black')], \
                selectbackground=[('readonly', '!focus', '#DCDAD5')])
    tMonths = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    cbDtRStM = objTTK.Combobox(master=objWindow, width=10, state="readonly", values=tMonths)
    cbDtRStM.pack()
    
    btnButton = objTTK.Button(master=objWindow, text="Test")
    btnButton.pack()
    
    objWindow.bind("<Escape>", lambda funcWinSum: objWindow.destroy())
    
    objWindow.mainloop()