C
This article answers how to respond.Same http://effbot.org/tkinterbook/listbox.htm#Tkinter.Listbox.itemconfig-method Specify the position and change contents. https://stackoverflow.com/q/5348454/9014308 2.x https://stackoverflow.com/q/36914176/9014308 If you quote the first article,. According effbot.org documentation http://effbot.org/tkinterbook/listbox.htm#Tkinter.Listbox The color of the spefic:According to the effbot.org document on the Listbox widget, the color of a specific item cannot be changed.You can use the same font and color.The list box can contain only text items, all items must be the same font and colorYou can change the fonts and background colors. http://effbot.org/tkinterbook/listbox.htm#Tkinter.Listbox.itemconfig-method method Listbox Example:However, in fact, you can use the itemconfig method of the Listbox object to change both the font and the background color of a particular item. 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()
The above execution results are:If you select a line from here, it will be a blue background line. listboxIt seems that even if the character color/background color is changed, the font and boldness of the specified line are not possible. https://stackoverflow.com/q/53560889/9014308 You cannot change the font of an individual item.If you have any questions, please contact us. ttk.Treeview widgetYou cannot change fonts for individual items in the list box.If you need something that works like a list box but offers the ability to change fonts for individual items, you can use the ttk.Treeview widget.ttk.Treeview https://stackoverflow.com/q/40237671/9014308 https://stackoverflow.com/q/26957845/9014308 Note that ttk.Treeview widgets are also available.I added font processing below. https://ja.stackoverflow.com/q/64095/26370 #Please note that only one piece####4 partsimport tkinter as tk
from tkinter import ttk
以下関数が対処のための処理
def fixed_map(option):
return [elm for elm in style.map('Treeview', query_opt=option) if
elm[:2] != ('!disabled', '!selected')]
if name == 'main':
obj = ttk.tkinter.Tk()
# 以下2行が対処の必要な処理 & 上記関数を呼んでいる
style = ttk.Style()
style.map('Treeview', foreground=fixed_map('foreground'), background=fixed_map('background'))
tree = ttk.Treeview(
obj,
show = "headings",
)
tree_item = {
"No.":40,
"Name":80,
}
tree["columns"]=tuple(range(1,len(tree_item)+1))
for i, item in enumerate(tree_item.items()):
name, width = item
tree.heading(i+1,text=name)
tree.column(i+1,minwidth = width, width = width, stretch = False, anchor = tk.CENTER)
value_list = [
'aaa','bbb','ccc','ddd','eee','fff',
]
for i,v in enumerate(value_list):
tree.insert("",index = "end",tags = i,value=[i+1,v])
if i%2 == 0:
tree.tag_configure(i,background = 'yellow') # ここで色を変えている
#### 以下がフォントを変えている部分。上記と組み合わせて1回の呼び出しで両方変えても良い。
if i == 2:
tree.tag_configure(i,font = 'Arial 16 bold')
else:
tree.tag_configure(i,font = 'Courier 16')
tree.pack()
obj.mainloop()
This is the result.