Согласно документации effbot.org
относительно виджета Listbox
, вы не можете изменить цвет специальных предметов:
Список может содержать только текстовые элементы, и все элементы должны иметь одинаковый шрифт и цвет
Но на самом деле вы можете изменить как шрифт, так и цвет фона определенных элементов, используя itemconfig
метод вашего Listbox
объекта. Смотрите следующий пример:
import tkinter as tk
def demo(master):
listbox = tk.Listbox(master)
listbox.pack(expand=1, fill="both")
# inserting some items
listbox.insert("end", "A list item")
for item in ["one", "two", "three", "four"]:
listbox.insert("end", item)
# this changes the background colour of the 2nd item
listbox.itemconfig(1, {'bg':'red'})
# this changes the font color of the 4th item
listbox.itemconfig(3, {'fg': 'blue'})
# another way to pass the colour
listbox.itemconfig(2, bg='green')
listbox.itemconfig(0, foreground="purple")
if __name__ == "__main__":
root = tk.Tk()
demo(root)
root.mainloop()