JDM
JDM

Reputation: 1804

Directly modify a specific item in a TKinter listbox?

This is one of those just-making-sure-I-didn't-miss-anything posts.

I have a TKinter GUI in Python 2.7.3 that includes a listbox, and there are circumstances where I'd like to directly modify the text of a specific item at a known index. I've scoured the documents and there's no lb.itemset() method or anything like it. As best I can tell I have two options, either of which would work but just seem kind of klunky to me:

  1. lb.delete() the old item and lb.insert() the new value for it at the same index (including a step to re-select the new value if the old deleted one happened to be selected).

  2. Create a listvariable for the listbox, then use get() and set() on it -- with a pile of replace/split/join acrobatics in between to handle the differing string formats involved.

Is there some simpler, more direct way to do it that I'm missing? Or have I turned up all the available options?

Upvotes: 3

Views: 1420

Answers (1)

JDM
JDM

Reputation: 1804

Assuming from silence that there's nothing I missed. I went with option 2 -- the acrobatics weren't quite as complex as I'd thought. I just created a behind-the-scenes list wrapped up in a class; every time I update the list, the class syncs up the content of the listbox by doing a ' '.join on the list then setting the listbox's listvariable to the resulting string.

Upvotes: 2

Related Questions