[Tkinter-discuss] Binding a List to a ScrollBar

Michael O'Donnell michael.odonnell at uam.es
Thu Jan 16 14:44:21 CET 2014


Hi Rob,

Complete solution, with a re-usable ScrolledListBox class,
below:

from tkinter import *

class ScrolledListBox(Frame):

    def __init__(self, master, items=[], width=24, height=20,
bgcol="white", **keywords):
        Frame.__init__(self, master)
        self.config(bg=bgcol)

        # Scrollbars
        scrollbar = Scrollbar(self, orient=VERTICAL)

        # Create the listbox
        self.table=Listbox(self, yscrollcommand=scrollbar.set)
        self.table.config(bg = bgcol, width=width, height=height)
        scrollbar.config(command=self.table.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.table.pack(side=LEFT, fill=BOTH, expand=1)

        for kw in keywords:
            self.table[kw]=keywords[kw]

        # insert the items
        for item in items:
            self.addItem(item)

    def addItem(self, item, bg=None):
        self.table.insert(END, item)
        if bg: self.table.itemconfig(END, background=bg)


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()

sb=ScrolledListBox(root, [])
sb.pack(side=LEFT)
for c in COLORS:
    sb.addItem(c, c)

root.mainloop()



On 16 January 2014 09:50, Nicco Kunzmann <niccokunzmann at rambler.ru> wrote:

>  Hello Rob,
>
> as far as I remember you can only insert text into a Listbox.
> Maybe you pack Widgets onto a Listbox they appear but it does not make
> them items.
> Try this:
>     mylist.insert(END,*c* )
>     mylist.itemconfigure(END, background = c)
> If you want to change the background.
>
> Greetings,
> Nicco
>
> Am 16.01.2014 07:06, schrieb Rob Ward:
>
> 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 listTkinter-discuss at python.orghttps://mail.python.org/mailman/listinfo/tkinter-discuss
>
>
>
> _______________________________________________
> 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/b10338e8/attachment.html>


More information about the Tkinter-discuss mailing list