【问题标题】:Changing colour of item in Tkinter listbox更改 Tkinter 列表框中项目的颜色
【发布时间】:2016-08-23 04:31:45
【问题描述】:
【问题讨论】:
标签:
python
tkinter
listbox
【解决方案1】:
itemconfig() 的
index 参数可以是"end",你应该利用它。先将项目插入end,然后更改其背景。
import Tkinter as tk
def demo(master):
listbox = tk.Listbox(master)
listbox.pack(expand=1, fill="both")
# inserting some items
for names in [0,1,-2,3,4,-5,6]:
listbox.insert("end", names)
listbox.itemconfig("end", bg = "red" if names < 0 else "green")
#instead of one-liner if-else, you can use common one of course
#if item < 0:
# listbox.itemconfig("end", bg = "red")
#else:
# listbox.itemconfig("end", bg = "green")
if __name__ == "__main__":
root = tk.Tk()
demo(root)
root.mainloop()