Replace single item in tkinter ListBox

jepler at unpythonic.net jepler at unpythonic.net
Sun Dec 19 04:59:54 CET 2004


To the best of my knowledge, the only way to do this is as you describe: delete
the old item, then insert the new item.  The Tk manpage for "listbox" doesn't
describe any commands to only update an item in the list.

Actually, you may have another choice, if your Tk is new enough---Tk supports
(in 8.4, and maybe 8.3) the -listvariable configuration option on listboxes.

.>>> t = Tkinter.Tk()
.>>> v = Tkinter.Variable(t)
.>>> l = Tkinter.Listbox(t, listvariable=v); l.pack()
.>>> v.set((1,2,3))

Now, if you assemble a Python sequence 's', you can set the list's contents
to that sequence with
.>>> v.set(tuple(s))
this still doesn-t allow slice-like updating of listboxes, however.

Finally, you could subclass the Tkinter.Listbox, and replace the __setitem__
and __setslice__ methods with methods that manipulate the underlying listbox
in the appropriate ways, then create these objects instead of Tkinter.Listbox.
I'll leave that as an exercise for the reader.  Feel free to contribute the code
to the Tkinter wiki (http://tkinter.unpy.net).

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20041218/398d9a05/attachment.pgp>


More information about the Python-list mailing list