python tkinter treeview 将焦点设置到新插入的项目

python tkinter treeview set focus to newly inserted item

我正在插入一个新项目,例如:

# Insert new item as child of current item
tree.insert(current_node_iid, 1, text="New note")

我可以通过以下方式将焦点设置在一个项目上:

# Must select and then focus using the iid of an item
tree.selection_set("I001")
tree.focus("I001")

但是如果我插入一个新项目,然后让 Python 设置新项目的索引,那么我不知道这个新索引,所以我无法将焦点设置在新项目上。

我当然可以指定新项目的 iid,但我必须检查 iid 是否已被另一个项目使用,我不知道该怎么做。

如何设置新项目的焦点?

感谢任何帮助。提前致谢。

只需为您的 tree.insert 命令定义一个变量。

a = tree.insert(current_node_iid, 1, text="New note")
print (a)

结果:

I001