pythontkintercomboboxtk-toolkit

ttk.Combobox selected text vanishes when focus is lost (clam theme, readonly)


I'm new to python and recently started coding a combobox using the "clam" UI theme. However, the selected text vanishes when focus is lost from the dropdown? It seems confined to "clam" as other UI themes such as "alt" work fine.

For example, the initial screen shows

screen1

Click the dropdown icon

screen2

Now, re-click the dropdown icon

screen3

Notice the default value apples has vanished? I was expecting it to be visible.

Example Code

import tkinter as tk, tkinter.ttk as ttk

root = tk.Tk()

style = ttk.Style(root)
style.theme_use("clam")
style.configure("TCombobox", fieldbackground="white", background="white")
style.map("TCombobox", fieldbackground=[("readonly", "white")], background=[("readonly", "white")])

cb = ttk.Combobox(root, state="readonly", values=("apples", "oranges", "bananas"))
cb.set("apples")
cb.pack(padx=40, pady=40)

root.mainloop()

Solution

  • It is because the default color for foreground when readonly and focus is "white" for "clam" theme, so it is invisible when the fieldbackground is "white".

    You can set the color for foreground when readonly and focus to "black":

    style.map("TCombobox",
        fieldbackground=[("readonly", "white")],
        background=[("readonly", "white")],
        foreground=[("readonly", "focus", "black")], # or [("readonly", "black")]
    )