我有一个基于Python 3和tkinter的GUI,它有一个很大的ttk.Treeview
。我已经定义了行选择方法(单击一下)并打开高级信息面板(双击)。我需要确保在双击后,在接下来的一两秒内,Treeview状态不会被另一次点击更改。是否可以取消激活Treeview鼠标绑定,就像我们使用按钮一样?
答案 0 :(得分:0)
做了一点研究,我能够为此提出解决方案。我刚刚创建了一个空方法,当树小部件应该处于非活动状态时调用该方法。所以,我们可以使用这样的东西来解开"解开"所有鼠标事件,并在几秒钟后根据需要重新绑定它们:
def nothing(self, *event):
""" # Hacking moment: A function that does nothing, for those times you need it...
"""
pass
def bind_tree(self):
""" # Bind mouse and keyboard events to their respective functions or methods...
"""
self.tree.bind('<<TreeviewSelect>>', self.selectItem_popup)
self.tree.bind('<Double-1>', self.show_details)
self.tree.bind("<Button-2>", self.popupMenu)
self.tree.bind("<Button-3>", self.popupMenu)
def unbind_tree(self):
""" # Unbind all mouse and keyboard events, by binding them to an empty method...
"""
self.tree.bind('<<TreeviewSelect>>', self.nothing)
self.tree.bind('<Double-1>', self.nothing)
self.tree.bind("<Button-2>", self.nothing)
self.tree.bind("<Button-3>", self.nothing)
然后,在其余代码中,我们只需要根据需要调用bind_tree()
和unbind_tree()
。
答案 1 :(得分:0)
这对我有用:
tree.bind("<ButtonRelease-1>", my_select_function)
# Do some stuff
tree.unbind("<ButtonRelease-1>")