Search code examples
python-3.xtkintercombobox

How to set up tkinter Combobox border color


I'm looking for a way to modify tkinter Combobox border color and thickness and cannot find a way how to do it.

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x350")

style= ttk.Style()
style.theme_use('clam')    # why combobox style works only when theme is used???
style.configure("test1.TCombobox", fieldbackground= "red")
#style.configure("test2.TCombobox", How to set up border color & thickness???)

label=ttk.Label(win, text= "Select a Car",
font= ('Aerial 11'))
label.pack(pady=30)

cb= ttk.Combobox(win, width= 25, values=["Honda", "Hyundai", "Wolkswagon", "Tata", "Renault", "Ford", "Chrevolet", "Suzuki","BMW", "Mercedes"])#, style="test1.TCombobox")
cb.pack()

cb['style'] = "test1.TCombobox"     # I need to be able to set up style after widget is initialized

win.mainloop()

In following code example I'm able to modify background but I cannot find how to modify border. Can someone smarter than me help me fill in test2.TCombobox style for red border with some thickness?

Second question. Why setting up background stops working when I comment theme???

style.theme_use('clam')    # why combobox style works only when theme is used???

Edit: Second question answer: Looks like only setting style enables widget's many styling options (that's how I understand documentation).


Solution

  • here with "clam" . Another possibility is, for example, "default" and then play around with "borderwidth". i will show you just one example of many. in this case, "light" and "darkcolor" are changed. thus the "bordercolor" is changed. there is only one possibility, there are several more.

    from tkinter import *
    from  tkinter import ttk
    root = Tk()
    root.geometry("200x100")
    root.option_add("*TCombobox*Listbox*Background", "#1d2128")
    root.option_add("*TCombobox*Listbox*Foreground", "#8b9ebf")
    root.option_add("*TCombobox*Listbox*Font", "Courier")
    
    style = ttk.Style()
    style.theme_use("clam")
    style.configure('TCombobox',
                             background="#1d2128",
                             fieldbackground="#1d2128",
                            
                             foreground="#8b9ebf",
                             darkcolor="lime",
                             selectbackground="grey",
                             lightcolor="lime"
                             )
    
    opts = ["Hello", "Bye"]
    cb = ttk.Combobox(root, values=opts)
    cb.pack()
    
    root.mainloop()