我指的是 Listbox 小部件中的特定元素。
I'm referring to a specific element in the Listbox widget.
着色背景是最期望的,但任何形式的着色对于特定的单元格将是太棒了。
Colouring the background is most desired but any form of colouring for a specific cell would be fantastic.
推荐答案根据 effbot 文档Tkinter.Listboxrel =nofollow> Listbox 小部件不能更改特定项目的颜色:
According to the effbot documentation regarding the Listbox widget you cannot change the color of spefic items:
列表框只能包含文本项,所有项必须具有相同的字体和颜色。
The listbox can only contain text items, and all items must have the same font and color
但实际上你可以改变特定项目的字体和背景颜色,通过使用 Listbox 对象的 c> itemconfig 请参阅以下示例:
But actually you can change both the font and background colors of specific items, by using the itemconfig method of your Listbox object. See the following example:
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()更多推荐
是否可以在Listbox小部件中为特定项目着色?
发布评论