PDA

View Full Version : Python change the color of substring in Tkinter



DBQ
June 17th, 2011, 06:51 PM
Hi Everybody.

I am using tkinter in python. I have a listbox object. Is there a way to set one part of the item to one color and the other part to another?

For example, the listbox contains a single item "Hello World". Is there a way to make "Hello" one color and "World" the other?

Thank You!

cgroza
June 17th, 2011, 08:20 PM
Check the class reference at:
http://epydoc.sourceforge.net/stdlib/Tkinter.Listbox-class.html

Listbox.itemconfig() seems to be what you need...

DBQ
June 17th, 2011, 08:24 PM
I think this method can only be used to change the color of the whole item, not a part of it. Am I wrong?

juancarlospaco
June 17th, 2011, 08:27 PM
Listbox.itemconfig(bg='black', fg='white')

DBQ
June 17th, 2011, 08:31 PM
What I am trying to do is to change PART of the item's foreground color. Like if ONE item is "Hello World" I want "Hello" to be one color and "World" another.

cgroza
June 17th, 2011, 08:35 PM
What I am trying to do is to change PART of the item's foreground color. Like if ONE item is "Hello World" I want "Hello" to be one color and "World" another.
I think you will have to inherit and write your own widget to do that.

DBQ
June 17th, 2011, 09:10 PM
What is the easiest way to achieve this? Can you give any tips.

Also, if there is another way to do it in Tkinter I would love to know.

Thank you!

juancarlospaco
June 17th, 2011, 09:28 PM
You can, but not with Listbox, maybe Text