How to change the text of a Listbox item?

I have a Listbox full of items, and I need to change the text of the item. Using the configure element, I can only learn how to change colors.

How to change item text on Tkinter Listbox ?

+4
source share
2 answers

To change the text, you will need to remove and re-add the item at the corresponding index.

Here's a contrived example that continuously updates the second item in a list:

 import Tkinter as tk import time class Example(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) self.lb = tk.Listbox(self) self.lb.pack(fill="both", expand=True) self.lb.insert("end", "item 1","the current time", "item 3") self.after(1000, self._update_listbox) def _update_listbox(self): self.lb.delete(1) self.lb.insert(1, time.asctime()) self.after(1000, self._update_listbox) if __name__ == "__main__": root = tk.Tk() Example(root).pack(fill="both", expand=True) root.mainloop() 
+2
source

You must first delete the old item (by specifying its index) using the delete method of your Listbox object:

 myList.delete(index, old_item) 

and then insert your updated_item at the position of your old element:

 myList.insert(index, updated_item) 
+2
source

Source: https://habr.com/ru/post/1490329/


All Articles