Tkinter Listbox - Different Text colors in one listbox

Eric Brunel see.signature at no.spam
Wed May 30 04:04:56 EDT 2007


On Tue, 29 May 2007 19:02:03 +0200, <rahulnag22 at yahoo.com> wrote:

> Hi,
> Is it possible to have different items in a listbox in different
> colors? Or is it just one color for all items in a listbox?
> Thanks
> Rahul
>

AFAIK, this is not possible with a listbox. You can however quite easily  
emulate the behaviour of a listbox with a text widget, which allows to mix  
fonts and colors in any way you like.

I did it once by creating a sub-class of Tkinter.Text (cannot post the  
code here - closed source, sorry...) and all I had to do was:
- make sure the text widget had its state to DISABLED all the time, except  
when modifying it;
- removing all the bindings defined in the text widgets (use  
widget.bind_class('Text') to get all the events, then widget.bind(event,  
lambda e: 'break') to remove them);
- define a new binding for a button click selecting the line under the  
cursor;
- define the insert, delete and getcurselection methods, taking care of  
treating the special index END.

All in all, this was just a few dozen lines.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list