【问题标题】:How can I change the text of Listbox item?如何更改列表框项目的文本?
【发布时间】:2013-07-06 00:30:41
【问题描述】:

我有一个充满项目的Listbox,我需要更改项目的文本。使用项目配置我只能找出如何更改颜色。

如何更改 Tkinter Listbox 上的项目文本?

【问题讨论】:

    标签: python listbox tkinter


    【解决方案1】:

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

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

    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】:

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

      myList.delete(index, old_item)
      

      然后insert你的 updated_item 在你旧项目的位置:

      myList.insert(index, updated_item)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-11
        • 2022-01-14
        相关资源
        最近更新 更多