如何在python中将动作绑定到tkinter树视图的标题? [英] How to bind an action to the heading of a tkinter treeview in python?

查看:264
本文介绍了如何在python中将动作绑定到tkinter树视图的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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?

推荐答案

您可以将事件绑定到Treeview小部件本身.窗口小部件具有名为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆