Python: Tkinter Treeview Searchable

后端 未结 3 1870
一个人的身影
一个人的身影 2021-02-06 16:21

Fairly straight forward question, and despite my best Google-Fu I can\'t find anything on this.

I have a Python app that uses a Tkinter Treeview widget as a table. This

3条回答
  •  清歌不尽
    2021-02-06 17:11

    And, for those who want a Tcl-based solution, here is one:

     proc searchtree {} {
    
        set children [.tree children {}]
        foreach id $children {
        set text [.tree item $id -text]
        if {$text eq [.e get]} {
            .tree selection set $id
    
        } else  {
            set children [.tree children $id]
            foreach id $children {
            set text [.tree item $id -text]
            if {$text eq [.e get]} {
                .tree selection set $id
            }
            }
        }
        .tree see $id
        }
    }
    
    
    pack [ttk::treeview .tree]
    
    # Inserted at the root, program chooses id:
    .tree insert {} end -id widgets -text "Widget Tour"
    
    # Same thing, but inserted as first child:
    .tree insert {} 0 -id gallery -text "Applications"
    
    # Treeview chooses the id:
    set id [.tree insert {} end -text "Tutorial"]
    
    # Inserted underneath an existing node:
    .tree insert widgets end -text "Canvas"
    .tree insert $id end -text "Tree"
    
    
    pack [button .b -text "Search" -command searchtree]
    pack [entry .e ]
    

提交回复
热议问题