【问题标题】:tkinter treeview click event for selected item [duplicate]所选项目的tkinter treeview单击事件[重复]
【发布时间】:2017-03-24 20:20:49
【问题描述】:

在我的小型 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"))

树的结构:

【问题讨论】:

  • 只需在OnDoubleClick 中添加一个检查,如果用户没有点击最后一项,则什么也不做。
  • 在我的情况下,我只想对 second_child 元素进行点击事件(例如:Amount)。据我了解,我需要检查在 OnDoubleClick 中单击了哪些元素,是吗?你能用很少的代码解释一下吗?我有点困惑。
  • @BryanOakley 你能再次检查我的代码吗?我更新我的代码仍然不知道 OnDoubleClick 有什么问题。

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


【解决方案1】:

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 “>” event.

每当打开一个项目时,小部件都会生成一个“>”事件。

每当关闭一个项目时,小部件都会生成一个“>”事件。

【讨论】:

  • 如何将该事件映射到视图中的项目?
猜你喜欢
  • 2020-09-24
  • 2011-04-17
  • 2012-08-16
  • 2017-09-25
  • 2016-11-20
  • 2020-10-30
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多