By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Replace single item in tkinter ListBox

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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-----

Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.