gpt4 book ai didi

python - 是否可以为列表框小部件中的特定项目着色?

转载 作者:太空狗 更新时间:2023-10-29 21:16:59 25 4
gpt4 key购买 nike

我指的是 Listbox 小部件中的特定元素。

为背景着色是最理想的,但为特定单元格着色的任何形式都很棒。

最佳答案

根据关于 Listboxeffbot.org 文档您无法更改特定项目颜色的小部件:

The listbox can only contain text items, and all items must have the same font and color

但实际上您可以使用 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()

关于python - 是否可以为列表框小部件中的特定项目着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5348454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com