[Tkinter-discuss] Binding a List to a ScrollBar

Michael Lange klappnase at web.de
Thu Jan 16 10:10:20 CET 2014


Hi,

On Thu, 16 Jan 2014 17:06:26 +1100
"Rob Ward" <rl.ward at bigpond.com> wrote:

> Hi Folks,
> Sorry to be back so soon but I have another challenge.

No problem, that's what this list was made for ;)

(...)

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

You are trying here to use the Listbox as a kind of "scrollable Frame"
widget which apparently does not work as expected.
If you really want to have a scrolled Frame widget in Tkinter you will
have to use some "3rd party" widget; I never actually used it myself, but
I think the ScrolledWindow widget that comes with the standard
library's Tix module should be usable. There is also a ScrolledFrame
widget that comes with Pmw (http://pmw.sf.net). There are also some
"standalone" ScrolledFrame widgets available, that may or may not be
Python3-compatible.

For your particular case it looks like the good news is that you need
none of these (at least if I understood correctly what you want to
achieve), because you can simply make use of the Listboxes itemconfigure
command, as in:

mylist = Listbox(root, yscrollcommand = scrollbar.set )
i = 0
for c in COLORS:
    mylist.insert(END,c)
    mylist.itemconfigure(i, background=c)
    i += 1

If you are really ambitious you can even try to change each item's
foreground, selectbackground and selectforeground colors appropriate to
its background.

Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Blast medicine anyway!  We've learned to tie into every organ in the
human body but one.  The brain!  The brain is what life is all about.
		-- McCoy, "The Menagerie", stardate 3012.4


More information about the Tkinter-discuss mailing list