我有一个装满项目的 Listbox
,需要改变一个项目的文本。使用item configure
我只能找到如何更改颜色。
如何在 Tkinter 的 Listbox
中更改项目文本?
要更改文本,您需要删除并在适当的索引位置重新添加一个项。
下面是一个人为制造的示例,它不断更新列表框中的第二个项目:
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()