How to make ttk.Treeview's rows editable?

后端 未结 4 840
攒了一身酷
攒了一身酷 2020-12-06 06:44

Is there any way to use ttk Treeview with editable rows?

I mean it should work more like a table. For example on double click on the it

4条回答
  •  爱一瞬间的悲伤
    2020-12-06 07:09

    This is just for creating a tree for the specified path that is set in the constructor. you can bind your event to your item on that tree. The event function is left in a way that the item could be used in many ways. In this case, it will show the name of the item when double clicked on it. Hope this helps somebody.

        import ttk
        from Tkinter import*
        import os*
    
        class Tree(Frame):
    
        def __init__(self, parent):
            Frame.__init__(self, parent)
            self.parent = parent
            path = "/home/...."
            self.initUI(path)
    
        def initUI(self, path):
            self.parent.title("Tree")
            self.tree = ttk.Treeview(self.parent)
            self.tree.bind("", self.itemEvent)
            yScr = ttk.Scrollbar(self.tree, orient = "vertical", command = self.tree.yview)
            xScr = ttk.Scrollbar(self.tree, orient = "horizontal", command = self.tree.xview)
            self.tree.configure(yscroll = yScr.set, xScroll = xScr.set)
            self.tree.heading("#0", text = "My Tree", anchor = 'w')
            yScr.pack(side = RIGHT, fill = Y)
    
            pathy = os.path.abspath(path) 
            rootNode = self.tree.insert('', 'end', text = pathy, open = True)
            self.createTree(rootNode, pathy)
    
            self.tree.pack(side = LEFT, fill = BOTH, expand = 1, padx = 2, pady = 2)
    
            self.pack(fill= BOTH, expand = 1) 
    
        def createTree(self, parent, path)
            for p in os.listdir(path)
                pathy = os.path.join(path, p)
                isdir = os.path.isdir(pathy)
                oid = self.tree.insert(parent, 'end' text = p, open = False)
                if isdir:
                   self.createTree(oid, pathy)
    
        def itemEvent(self, event):
            item = self.tree.selection()[0] # now you got the item on that tree
            print "you clicked on", self.tree.item(item,"text")
    
    
    
        def main():
            root = Tk.Tk()
            app = Tree(root)
            root.mainloop()
    
        if __name__ == '__main__'
           main()
    

提交回复
热议问题