Jung-suk
Jung-suk

Reputation: 172

CTRL + a in treeview widget - Python

Please help me with the idea to select all row at once using ctrl+a key in treeview widget tkinter python.Thank you.

import tkinter as tk
from tkinter import ttk


root = tk.Tk()
cols = ('name','age')
e = ttk.Treeview(root,columns=cols)
for col in cols:
            e.heading(col, text=col)
            e.column(col,minwidth=0,width=170)
e.pack()
e.insert("","end",values=("ss",66))
e.insert("","end",values=("yy",11))
root.mainloop()

Upvotes: 3

Views: 471

Answers (2)

Sinkus
Sinkus

Reputation: 1

import tkinter
from tkinter import ttk

root = tkinter.Tk()

tree = ttk.Treeview(root, height=15)
tree.grid(column=0, row=0, padx=1, pady=1)

tree["columns"] = ("Manufacturer", "Model", "Electric")

tree.column("#0", width=0, minwidth=0)  # "Phantom" Column is always created with a new Treeview
tree.column("0", width=100, minwidth=100)
tree.column("1", width=100, minwidth=100)
tree.column("2", width=100, minwidth=100)

tree.heading("#0", text="")
tree.heading("0", text="Manufacturer")
tree.heading("1", text="Model")
tree.heading("2", text="Electric")

tree.insert(parent="", index="end", values=("Mercedes-Benz", "EQS", "Yes"))
tree.insert(parent="", index="end", values=("Hyundai ", "IONIQ 5", "Yes"))
tree.insert(parent="", index="end", values=("BMW ", "M5", "No"))


def select_all(event):
    # Get all of the row ids that are present in your tree
    item_ids = tree.get_children()

    # Select all the items in the treeview
    tree.selection_set(item_ids)


# You can bind keyboard strokes or mouse clicks to your treeview
# structure in order to execute specific method
tree.bind('<Control-a>', select_all)

root.mainloop()

Upvotes: 0

Stefan Scheller
Stefan Scheller

Reputation: 953

You have to bind a callback function to the according keyboard event. The function itself needs access to the Treeview widget. You can do this by binding e in a closure:

root.bind('<Control-a>', lambda *args: e.selection_add(e.get_children()))

Upvotes: 4

Related Questions