是否可以为 Listbox 小部件中的特定项目着色?

编程入门 行业动态 更新时间:2024-10-20 03:28:18
本文介绍了是否可以为 Listbox 小部件中的特定项目着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我指的是 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 文档.htm#Tkinter.Listbox" rel="noreferrer">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 对象的noreferrer">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 小部件中的特定项目着色?

本文发布于:2023-11-27 03:46:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   项目   Listbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!