如何将动作绑定到 python 中 tkinter 树视图的标题? [英] How to bind an action to the heading of a tkinter treeview in python?
问题描述
我正在使用 tkinter Treeview
小部件来显示数据库.单击其中一个标题时的命令用于根据单击的列对表格进行排序.
I am using the tkinter Treeview
widget to show a database. The command when clicking on one of the headings is used for sorting the table based on the clicked column.
此外,我希望在我将鼠标悬停(或右键单击)其中一个标题时立即显示一个工具提示框.工具提示对于其他小部件不是问题,但树视图的标题当然不是完整的小部件.
Additionally I want a tooltip box show up as soon as I hover (or right click) over one of the headings. The tooltips aren't a problem for other widgets but the heading of a treeview isn't a full widget of course.
除了通常的命令之外,我如何将任何操作绑定到标题?
How can I bind any action to the headings except for the usual command?
推荐答案
您可以将事件绑定到树视图小部件本身.该小部件有一个名为 identify 的方法这可用于确定事件发生在树视图的哪个部分.
You can bind the events to the treeview widget itself. The widget has a method named identify which can be used to determine which part of the treeview the event occurred over.
例如:
...
self.tree = ttk.Treeview(...)
self.tree.bind("<Double-1>", self.on_double_click)
...
def on_double_click(self, event):
region = self.tree.identify("region", event.x, event.y)
if region == "heading":
...
这篇关于如何将动作绑定到 python 中 tkinter 树视图的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!