Tkinter Listbox - Different Text colors in one listbox

Discussion in 'Python' started by rahulnag22@yahoo.com, May 29, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Guest

    On May 29, 12:02 pm, 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/tkinter/introduction/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, 2007
    #2
    1. Advertising

  3. <> 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


    You specify text and foreground colour when you make the box,
    so I don't think its possible.

    - Hendrik
    Hendrik van Rooyen, May 30, 2007
    #3
  4. Eric Brunel Guest

    On Tue, 29 May 2007 19:02:03 +0200, <> 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+-'])"
    Eric Brunel, May 30, 2007
    #4
  5. Guest

    On May 29, 2:02 pm, 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, 2007
    #5
  6. Guest

    On May 30, 6:04 am, wrote:
    > On May 29, 2:02 pm, 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, 2007
    #6
  7. <> wrote:


    > On May 29, 2:02 pm, 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
    Hendrik van Rooyen, May 31, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    552
    Thomas J. Gritzan
    Oct 6, 2006
  2. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    536
    darrel
    Aug 7, 2008
  3. Replies:
    2
    Views:
    1,153
    Göran Andersson
    Oct 18, 2008
  4. ZelluX
    Replies:
    3
    Views:
    1,880
    Peter Otten
    Dec 1, 2008
  5. Replies:
    3
    Views:
    185
    Rick Johnson
    Jan 11, 2013
Loading...

Share This Page