根据
effbot.org有关
Listbox小部件的文档,您不能更改特殊项目的颜色:
列表框只能包含文本项,并且所有项必须具有相同的字体和颜色
但是实际上,您可以使用对象的
itemconfig方法来更改特定项目的字体和背景色
Listbox。请参见以下示例:
import tkinter as tkdef 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()


