Replace single item in tkinter ListBox

Z

Zhang Le

Hello,
Is there a quick way to replace the content of a single item in
tkinter's listbox? Currently my solution is to first delete the item,
then insert a new item at the same position. I think there may be
better way.

Zhang Le
 
J

jepler

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFBxPy4Jd01MZaTXX0RAi4BAJ9E/NV2wZLFywnbVlEgIcAEX9NLRgCeKvsw
ijo04e2O2oG7X3ijqJlYdU0=
=rYNN
-----END PGP SIGNATURE-----
 
Z

Zhang Le

Thanks for the hint. But I did not try such *advance* techniques so
far.
Both Delete-then-insert and Insert-then-delete work for me.
Interestingly, I found Delete-then-insert can cause some flicking when
replacing items near bottom, while Insert-then-delete always works
fine.

Zhang Le
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top