0

I looked for and tested many similar questions/answers/possible duplicates here on SO and other sites but I'm interested specifically in using the solution below for simplicity's sake and minimal reproducible example constraint satisfaction.

Why does the following modification of this previous answer's code does not work/how to make it work?

import tkinter as tk
from tkinter import ttk
 
root = tk.Tk()
 
tree = ttk.Treeview(root)
tree.pack(fill="both", expand=True)
 
items = []
for i in range(10):
    item = tree.insert("", "end", text="Item {}".format(i+1))
    items.append(item)
 
items_to_select = items[:]
 
 
def select_all_items(event):
    tree.selection_set(items_to_select)
 
tree.bind('<Button-1>', select_all_items)
 
 
root.mainloop()

The original answer works as is.

I'm looking for a way to make it work with mouse event Button-1 click. The result should execute the selection of all items in the treeview when users click on any one item with the left mouse button (Button-1).

1
  • For later reference I added a button to trigger the selection and simplified the function using the get_children() method def select_all_items(event): tree.selection_set(tree.get_children()) with working script here: pastebin.com/3KWWFBwY (might not work with the loop version) Commented Nov 19, 2022 at 18:35

2 Answers 2

1

Since the default action of a mouse click on any row of the treeview will clear previous selections and select the clicked row.

To disable the default action, return "break" from the callback:

def select_all_items(event):
    tree.selection_set(items)
    return "break"

Then clicking any row in the treeview will select all rows in it.

Note that you can use items directly and don't need to clone it to new variable.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for letting me know about the default action behavior, I couldn't find that info. Just as a bonus where did you find that info? I'll be very grateful for the sharing of the doc (so I know where to look for for next needs thanks!). Here I did the testing in 3 video and yours solved it! i.imgur.com/THvX9sV.mp4 i.imgur.com/DDRXvqL.mp4 i.imgur.com/IT7VMJ4.mp4
See official document on bind.
Actually I learn this from answers in StackOverflow.
Good resource, I never knew.
Ok, I looked for hours on SO :)
1

Try this:

import tkinter as tk
from tkinter import ttk
 
root = tk.Tk()
 
tree = ttk.Treeview(root)
tree.pack(fill="both", expand=True)
 
items = []
for i in range(10):
    item = tree.insert("", "end", text="Item {}".format(i+1))
    items.append(item)
 
#items_to_select = []
    
for item in items:
    tree.insert('', tk.END, values=item) 
 
def select_all_items(event):
    for selected_item in tree.selection():
        item = tree.item(selected_item)
        print(item)
    #tree.selection_set(items_to_select)
 
tree.bind('<Button-1>', select_all_items)
  
root.mainloop()

You can also substitute for code. return "break" I commented out line 14.

import tkinter as tk
from tkinter import ttk
 
root = tk.Tk()
 
tree = ttk.Treeview(root)
tree.pack(fill="both", expand=True)
 
items = []
for i in range(10):
    item = tree.insert("", "end", text="Item {}".format(i+1))
    items.append(item)
 
#items_to_select = []
    
for item in items:
    tree.insert('', tk.END, values=item) 
 
def select_all_items(event):
     tree.selection_set(items)
     return "break"
 
tree.bind('<Button-1>', select_all_items)
  
root.mainloop()

2 Comments

Thanks for the reply but it's not solving it as is. Here I did the testing (yours is the 2nd one). Thanks any way for the proposed solution: i.imgur.com/THvX9sV.mp4 i.imgur.com/DDRXvqL.mp4 i.imgur.com/IT7VMJ4.mp4
Thanks again for the modification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.