I want to highlight that only one line of the listbox widget on tkinter using python is in red.

Asked 2 years ago, Updated 2 years ago, 105 views

Currently, we are using python and tkinter to create a program that receives an alert email and lists only the body.I am trying to update the python version of the original Excel vba, but I am using the listbox widget to display the list.I used listview for excel vba.

I have already received the mail and displayed the list, but I would like to highlight the mail that I should pay particular attention to in bold by changing the red color of the line, but it is difficult to do well.I don't think listbox can do that in the first place, and I would appreciate it if you could let me know if you know about it.

python tkinter

2022-09-30 19:31

1 Answers

This article answers how to deal with this problem.
They are all the same, specifying the location and changes in itemconfig().

Is it possible to collect a specific item in a Listbox widget?
By the way, this is a 2.x series Changing course of item in Tkinter listbox

The first article is quoted as follows:

Accoding to the effbot.org documentation registering the Listbox widget you cannot change the color of specific items:
According to the effbot.org documentation on the Listbox widget, you cannot change the color of a particular item.

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

But really 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<://example> However, in practice, you can use the itemconfig method in the Listbox object to change both the font and the background color of a particular item. See the following example:

import tkinter ask

def demo(master):
    listbox=tk.Listbox(master)
    listbox.pack(expand=1,fill="both")

    # inserting some items
    listbox.insert("end", "Allist 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 results are as follows:
If you choose a row from here, it will be a row with a blue background.
tkinter list color

In listbox, even if the character color/background color is changed, it seems that the font or bolding of specific lines is not possible.
How to change font to bold/underline/itatics in Python Tkinter listbox?

You cannot change the font of an individual item in a listbox.
If you need something that works like a listbox but which offers the possibility to change the font of individual items, you can use the ttk.Treeview widget.

You cannot change the font of individual items in the list box.
You can use the ttk.Treeview widget if you need something that works like a list box but provides the ability to change the font for individual items.

So I changed the font of ttk.Treeview in the article around here.
Pythontkinter single label with bold and normal text
ttk.Treeview-Can't change row height

However, be careful when using the ttk.Treeview widget.
I tried adding font processing below.
How to set the color for tags assigned to the list in Python tk.Treeview python 3.7
# Pay attention to only one part, ### and four parts for font processing

import tkinter ask
from tkinter import ttk

# The following functions are used for handling
defined_map(option):
    return [elm for elmin style.map('Treeview', query_opt=option)if
        elm [:2]!=('!disabled', '!selected')]

if__name__=='__main__':
    obj=ttk.tkinter.Tk()

    # The following two lines call the action & above functions that need to be addressed
    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', 'ffff',
    ]
    for i,vin enumerate(value_list):
        tree.insert("", index="end", tags=i, value=[i+1,v])
        if i%2 == 0:
            tree.tag_configure(i,background='yellow')#Change color here

        #### Below is the part where the font is changed.In combination with the above, both can be changed in one call.
        if i == 2:
            tree.tag_configure(i,font='Arial16bold')
        else:
            tree.tag_configure(i,font='Courier16')

    tree.pack()
    obj.mainloop()

This is the result.
color and bold


2022-09-30 19:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.