简体   繁体   English

Ttk Treeview:跟踪键盘选择

[英]Ttk Treeview: track keyboard selection

Here is a Tk widget with a ttk treeview. 这是带有ttk树视图的Tk小部件。 When user clicks on the row, some function is executed (here it just prints item text). 当用户单击该行时,将执行某些功能(此处仅打印项目文本)。 What I need is the following: 我需要的是以下内容:

  1. Initially a focus is on the text entry. 最初,重点是文本输入。 When user presses Tab key, the focus should go to the first row, and the function bound to Click event should be executed. 当用户按下Tab键时,焦点应移至第一行,并且应执行绑定到Click事件的功能。
  2. When user goes up and down in a treeview using the keyboard, this function should be executed as well, once a next cell is selected. 当用户使用键盘在树状视图中上下移动时,一旦选择了下一个单元格,则也应执行此功能。

I can't find anything related to these questions. 我找不到与这些问题有关的任何东西。 Is ttk treeview capable to track keyboard? ttk treeview是否可以跟踪键盘? Thanks. 谢谢。

import tkinter as tk
import tkinter.ttk as ttk

class App(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.CreateUI()
        self.grid(sticky = (tk.N,tk.S,tk.W,tk.E))
        parent.grid_rowconfigure(0, weight = 1)
        parent.grid_columnconfigure(0, weight = 1)

    def CreateUI(self):
        tv = ttk.Treeview(self,yscrollcommand=sc.set,height=30)
        tv['columns'] = ('Name',)
        tv.heading("#0", text='Items')
        tv.column("#0", anchor="w",width=75)
        tv.heading('Name', text='Name')
        tv.column('Name', anchor='w', width=150)
        tv.grid(sticky = (tk.N,tk.S,tk.W,tk.E))
        self.treeview = tv
        self.treeview.bind('<1>',self.OnClick)

    def OnClick(self,event):
        rowid=self.treeview.identify_row(event.y)
        self.treeview.selection_set(rowid)
        it=self.treeview.selection()[0]
        print(self.treeview.item(it,'values')[0])

items=[]
for i in range(100):
    items.append([i,'Item %d' % i])

root=tk.Tk()
sv=tk.StringVar()
filt=tk.Entry(root,textvariable=sv)
filt.grid(row=0,column=0,sticky='nw')
sc=tk.Scrollbar(root)
sc.grid(row=1,column=1,sticky='ns')
item_list=App(root)
item_list.grid(row=1,column=0,sticky='ns')
sc.config(command=item_list.treeview.yview)
for i in range(len(items)):
    item_list.treeview.insert('', 'end', iid=str(i), text=items[i][0], values=(items[i][1],))
item_list.treeview.selection_set('0')

def update_filter(*args):
    global items,item_list,sv
    filtr=sv.get().lower()
    item_list.treeview.delete(*(item_list.treeview).get_children())
    for i in range(len(items)):
        if filtr in str(items[i][0]).lower() or filtr in str(items[i][1]).lower():
            item_list.treeview.insert('', 'end', iid=str(i), text=items[i][0], values=(items[i][1],))
    item_list.treeview.update()
    item_list.update()

sv.trace('w', update_filter)
filt.focus()
root.mainloop()

As I read from the comments, you need some function to extract the current selection (or a part of that) from your treeview, execute a trigger action for another treeview on the selection. 正如我从评论中看到的那样,您需要一些功能来从树视图中提取当前选择(或其中的一部分),并对选择上的另一个树视图执行触发操作。

Therefore you can perfectly use the virtual <<TreeviewSelect>> -Event @CommonSense suggested. 因此,您可以完美使用建议的虚拟<<TreeviewSelect>> -Event @CommonSense。

As per documentation : 根据文档

45.1. 45.1。 Virtual events for the ttk.Treeview widget ttk.Treeview小部件的虚拟事件

Certain state changes within a Treeview widget generate virtual events that you can use to respond to these changes; Treeview小部件内的某些状态更改会生成虚拟事件,您可以使用这些事件来响应这些更改。 see Section 54.8, “Virtual events”. 请参见第54.8节“虚拟事件”。

  • Whenever there is a change in the selection, either by items becoming selected or becoming unselected, the widget generates a “<&ltTreeviewSelect>>” event. 每当选择发生更改时,无论是通过选择项还是未选择项,小部件都会生成“ <&ltTreeviewSelect >>”事件。
  • Whenever an item is opened, the widget generates a “<&ltTreeviewOpen>>” event. 每当打开项目时,小部件都会生成一个“ <&ltTreeviewOpen >>”事件。
  • Whenever an item is closed, the widget generates a “<&ltTreeviewClose>>” event. 每当关闭项目时,小部件都会生成“ <&ltTreeviewClose >>”事件。

Then use ttk.treeview.focus() to get the currently selected iid . 然后使用ttk.treeview.focus()获得当前选择的iid ttk.treeview.item(ttk.treeview.focus()) will give you the item you need to operate on. ttk.treeview.item(ttk.treeview.focus())将为您提供您需要进行操作的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM