[Tkinter-discuss] Binding a List to a ScrollBar

Bryan Oakley bryan.oakley at gmail.com
Thu Jan 16 13:05:08 CET 2014


A listbox can only scroll text. So, to display a colored word you would use
the "itemconfig" method to change the background and foreground of each
element individually.

For example:

        for index, color in enumerate(colors):
            listbox.insert("end", color)
            listbox.itemconfig(index, foreground=color)

If you really want to create a list of widgets, you'll have to either embed
the widgets in a canvas, put the widgets in a frame and put the frame in a
canvas, or put the widgets in a text widget (canvas and text widgets being
the two widgets that support vertical scrolling)




On Thu, Jan 16, 2014 at 12:06 AM, Rob Ward <rl.ward at bigpond.com> wrote:

>  Hi Folks,
> Sorry to be back so soon but I have another challenge.  I wanted to choose
> some colours for my program and came across a program that claimed to list
> the colours in a nifty little Python program.  Cute I thought, I must try
> that. Quite a few hours later I am stumped.  Here is the code with only the
> first 26 colours as data -
>
> from tkinter import *
>
> COLORS  =['snow', 'ghost white', 'white smoke', 'gainsboro', 'floral
> white', 'old lace',
>     'linen', 'antique white', 'papaya whip', 'blanched almond', 'bisque',
> 'peach puff',
>     'navajo white', 'lemon chiffon', 'mint cream', 'azure', 'alice blue',
> 'lavender',
>     'lavender blush', 'misty rose', 'dark slate gray', 'dim gray', 'slate
> gray',
>     'light slate gray', 'gray', 'light grey']
>
> root = Tk()
> scrollbar = Scrollbar(root)
> scrollbar.pack( side = RIGHT, fill=Y )
>
> mylist = Listbox(root, yscrollcommand = scrollbar.set )
> for c in COLORS:
>     e = Label(mylist, text = c, background = c)
>     mylist.insert(END,e )
>     e.pack(fill=X)#Problem line????
>
> mylist.pack( side = LEFT, fill = BOTH )
> scrollbar.config( command = mylist.yview )
>
> mainloop()
> As you can see I have an interesting line shown as a "Problem line????".
> If this line is out I get a list of pairs of number showing the default 10
> lines and  the list of numbers is "scrollable".  However if the line is in
> as shown above, I get a list 26 lines long, with the lovely colours nicely
> shown.  If I drag the list to a shorter size, the scroll bar appears but
> its movement does not link with the list.  It goes up an down nicely but
> the list stays still.  This is rather inconvenient when trying to view the
> complete list of colours, which is 479 high!
>
> Thanks in anticipation of anyone being able to help me, it will be very
> much appreciated.
>
> Cheers, Rob
>
>
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20140116/291b9988/attachment.html>


More information about the Tkinter-discuss mailing list