8

I have a Listbox full of items, and I need to change an item's text. Using item configure I can only find out how to change colors.

How can I change the item text on a Tkinter Listbox?

4

2 回答 2

5

您必须首先使用对象的delete方法删除旧项目(指定其索引) Listbox

myList.delete(index, old_item)

然后insert你的项目位置的更新项目:

myList.insert(index, updated_item)
于 2013-07-08T16:21:56.213 回答
5

要更改文本,您必须在适当的索引处删除并重新添加项目。

这是一个人为的示例,它不断更新列表框中的第二项:

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()
于 2013-07-08T16:26:22.580 回答