How to set the background color of a ttk.Combobox in tkinter?



Tkinter supports ttk widget which is used to change the style and properties of any widget in a tkinter application. We can set the background color, foreground color, and other attributes of the Combobox widget by visiting the configure function in ttk and passing 'TCombobox' as the first parameter.

Example

In this example, we will set the background color of the Combobox widget by defining its values in the ttk widget.

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Define the style for combobox widget
style= ttk.Style()
style.theme_use('clam')
style.configure("TCombobox", fieldbackground= "orange", background= "white")

# Add a label widget
label=ttk.Label(win, text= "Select a Car Model",
font= ('Aerial 11'))
label.pack(pady=30)
# Add a Combobox widget
cb= ttk.Combobox(win, width= 25, values=["Honda", "Hyundai", "Wolkswagon", "Tata", "Renault", "Ford", "Chrevolet", "Suzuki","BMW", "Mercedes"])

cb.pack()

win.mainloop()

Output

Running the above code will open a window that will have a combobox widget to select an option from the list.

raja
Published on 08-Jun-2021 09:52:50
Advertisements