Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command for clicking on the items of a Tkinter Treeview widget?

Tags:

I'm creating a GUI with Tkinter, and a major part of the GUI is two Treeview objects. I need the contents of the Treeview objects to change when an item (i.e. a directory) is clicked twice.

If Treeview items were buttons, I'd just be able to set command to the appropriate function. But I'm having trouble finding a way to create "on_click" behavior for Treeview items.

What Treeview option, method, etc, enables me to bind a command to particular items and execute that command "on_click"?

like image 865
Rafe Kettler Avatar asked Sep 25 '10 14:09

Rafe Kettler


People also ask

How do you search in Treeview?

TreeView enables you to search for the nodes matching the specified string. To search for a node in TreeView, you can use Search or SearchAll method of C1TreeView class. The Search method takes string as a value, searches for the nodes using depth-first search and returns the node containing the searched string.

What is Treeview widget in tkinter?

Introduction to the Tkinter Treeview widget A Treeview widget allows you to display data in both tabular and hierarchical structures. To create a Treeview widget, you use the ttk.Treeview class: tree = ttk.Treeview(container, **options) A Treeview widget holds a list of items. Each item has one or more columns.

How do I edit a Treeview in Python?

The Treeview widget items can be edited and deleted by selecting the item using tree. selection() function. Once an item is selected, we can perform certain operations to delete or edit the item.


2 Answers

If you want something to happen when the user double-clicks, add a binding to "<Double-1>". Since a single click sets the selection, in your callback you can query the widget to find out what is selected. For example:

import tkinter as tk from tkinter import ttk  class App:     def __init__(self):         self.root = tk.Tk()         self.tree = ttk.Treeview()         self.tree.pack()         for i in range(10):             self.tree.insert("", "end", text="Item %s" % i)         self.tree.bind("<Double-1>", self.OnDoubleClick)         self.root.mainloop()      def OnDoubleClick(self, event):         item = self.tree.selection()[0]         print("you clicked on", self.tree.item(item,"text"))  if __name__ == "__main__":     app = App() 
like image 61
Bryan Oakley Avatar answered Oct 11 '22 22:10

Bryan Oakley


The previous solution fails when multiple elements are selected and the user uses SHIFT+CLICK (at least on a Mac).

Here is a better solution:

import tkinter as tk import tkinter.ttk as ttk  class App:     def __init__(self):         self.root = tk.Tk()         self.tree = ttk.Treeview()         self.tree.pack()         for i in range(10):             self.tree.insert("", "end", text="Item %s" % i)         self.tree.bind("<Double-1>", self.OnDoubleClick)         self.root.mainloop()      def OnDoubleClick(self, event):         item = self.tree.identify('item',event.x,event.y)         print("you clicked on", self.tree.item(item,"text"))  if __name__ == "__main__":     app = App() 
like image 24
Csaba Avatar answered Oct 11 '22 23:10

Csaba