473,409 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Tkinter Listbox - Different Text colors in one listbox

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

May 29 '07 #1
6 19899
On May 29, 12:02 pm, rahulna...@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
Looks like it has to be the same color and font:
http://www.pythonware.com/library/tk...on/listbox.htm

Maybe there's a custom listbox in the PMW or Tix modules?

It would appear that wxPython has a control that does allow different
colors (and likely, fonts) called wx.HtmlListBox. I assume it would
allow any kind of html, although I've never played with it for sure.
The demo seems to imply that though.

Mike

May 29 '07 #2
<ra********@yahoo.comwrote:

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
You specify text and foreground colour when you make the box,
so I don't think its possible.

- Hendrik

May 30 '07 #3
On Tue, 29 May 2007 19:02:03 +0200, <ra********@yahoo.comwrote:
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+-'])"
May 30 '07 #4
On May 29, 2:02 pm, rahulna...@yahoo.com wrote:
Hi,
Is it possible to havedifferentitems in alistboxindifferentcolors? Or is it justonecolor for all items in alistbox?
Thanks
Rahul
from Tkinter import *

root = Tk()
l = Listbox(root)
l.pack()
for x in range(10):
l.insert(END, x)
l.itemconfig(2, bg='red', fg='white')
l.itemconfig(4, bg='green', fg='white')
l.itemconfig(5, bg='cyan', fg='white')
root.mainloop()

You can _only_ configurate 'background', 'foreground',
'selectbackground', 'selectforegroud', not font :(

HTH

May 30 '07 #5
On May 30, 6:04 am, rfg...@gmail.com wrote:
On May 29, 2:02 pm, rahulna...@yahoo.com wrote:
Hi,
Is it possible to havedifferentitems in alistboxindifferentcolors? Or is it justonecolor for all items in alistbox?
Thanks
Rahul

from Tkinter import *

root = Tk()
l = Listbox(root)
l.pack()
for x in range(10):
l.insert(END, x)
l.itemconfig(2, bg='red', fg='white')
l.itemconfig(4, bg='green', fg='white')
l.itemconfig(5, bg='cyan', fg='white')
root.mainloop()

You can _only_ configurate 'background', 'foreground',
'selectbackground', 'selectforegroud', not font :(

HTH
Thanks for the feedback all. I tried the itemconfig method and it
works great.
Thanks

May 30 '07 #6
<rf****@gmail.comwrote:

On May 29, 2:02 pm, rahulna...@yahoo.com wrote:
Hi,
Is it possible to havedifferentitems in alistboxindifferentcolors? Or is it
justonecolor for all items in alistbox?
Thanks
Rahul

from Tkinter import *

root = Tk()
l = Listbox(root)
l.pack()
for x in range(10):
l.insert(END, x)
l.itemconfig(2, bg='red', fg='white')
l.itemconfig(4, bg='green', fg='white')
l.itemconfig(5, bg='cyan', fg='white')
root.mainloop()

You can _only_ configurate 'background', 'foreground',
'selectbackground', 'selectforegroud', not font :(
Live and learn, - was not aware you could do this - thanks, nice one.

- Hendrik

May 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
I'm having problems changing the text colors in an edit box in my program. Here's what I'm trying to do, under WM_CREATE I have the following: ClockFace = CreateWindowEx(0, "EDIT", "0.0.00",...
2
by: RickDee | last post by:
Does anybody know how to display different color text inside ListBox ? I was testing it with CreateGraphics and DrawString methods, but it does not work out. Thanks Regards
2
by: Nenad Dobrilovic | last post by:
Hi, can I have two lines with different colors in Windows ListBox control? Thank you in advance, Cheya
5
by: tlyczko | last post by:
Hello, I've searched on toggling divs and didn't really find what I was looking for, I saved a bunch of different threads. I have a form with several different text boxes for things like dates,...
9
by: vincent90152900 | last post by:
How to pop up different text base on the selected TreeNode? I want to pop up different Text base on the selected TreeNode of the TreeView component. So, I create a TreeView and a ModalPopupExtender....
1
by: Gerardo ARnaez | last post by:
Hi. I am writing a program to help determine coumadin regimens to look at the code: http://sourceforge.net/projects/coumadinregimen/ The issue is that I have a variable that I want the use to...
2
by: gnosys | last post by:
In ASP.Net 1.1 using C#, I'm trying to dynamically change the background colors of certain listbox items based on some criteria. For example:
2
by: Patrick McGilla | last post by:
I would like to use different link colors at different places on the same web page. Using CSS, I can't seem to differentiateand even tried using div statements to separate. Anyone got an idea? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.