python - tkinter Treeview 所选项目的点击事件

标签 python python-3.x user-interface tkinter treeview

在我的小型 tkinter 应用程序中,我有一个结构如下图所示的树。 我只想在用户双击树的最后一个项目(Amount1 或 Amount2 等)时才创建单击事件。不幸的是,当我单击树的任何项目时,单击事件会起作用。如何修复此行为?!

代码:

self.treeView.insert('', 'end', "parent", text=text)

first_child = self.treeView.insert("parent", 'end', text=text)

second_child = self.treeView
second_child.insert(first_child, 'end', "", text=text)
second_child.bind("<Double-1>", self.OnDoubleClick)

def OnDoubleClick(self, event):
    item = second_child.identify('item', event.x, event.y)
    print("you clicked on", second_child.item(item, "text"))

树的结构:

enter image description here

最佳答案

https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Treeview-events.html

Treeview 小部件中的某些状态更改会生成虚拟事件,您可以使用这些事件来响应这些更改;请参阅第 54.8 节“虚拟事件”。

Whenever there is a change in the selection, either by items becoming selected or becoming unselected, the widget generates a “<<TreeviewSelect>>” event.

Whenever an item is opened, the widget generates a “<<TreeviewOpen>>” event.

Whenever an item is closed, the widget generates a “<<TreeviewClose>>” event.

关于python - tkinter Treeview 所选项目的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533812/

相关文章:

python - 运行 Tf.Keras 模型时内存不足

Python,与 joblib : Delayed with multiple arguments 并行化

python - 对包含 B 或 I 标签的连续单词进行分组

python - 如何使用 2 个条件中的任何一个拆分行?

python - 运行 python 代码时发生某种错误

python-3.x - 如何在linux mint中将python3更改为默认值

javascript - 如何在ABP模板中使用_formwizard.scss?

java.rmi.NotBoundException

c# - 如何在winforms上绘制自己的进度条?

Python matplotlib 减小颜色条标签的大小