如何不用鼠标在treeview中使用键盘选择数据

2024-10-01 11:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,当我的光标位于输入框(entrybox.focus())中时,我想使用键盘上的F4键(GUI.bind('<F4>', directselection))直接选择我的树视图中的第一个数据,而不是使用鼠标

我不知道如何创建这个函数。我想用

def directselection(event = None):
    treeviewtable.focus()

但它不起作用


Tags: 数据函数event视图binddefgui鼠标
1条回答
网友
1楼 · 发布于 2024-10-01 11:24:51

使用.get_children()获取行ID列表,然后使用.selection_set()选择第一行:

def directselection(event=None):
    # get all row IDs
    idlist = treeviewtable.get_children()
    if idlist:
        # select and focus on first row
        treeviewtable.selection_set(idlist[0])
        treeviewtable.focus(idlist[0])
        # transfer keyboard focus to treeviewtable
        treeviewtable.focus_force()

相关问题 更多 >