我有一个使用 ttk.Treeview
实例的简单脚本,我用文件系统树的内容填充该实例.我想在单击(叶)项目时执行某个操作,所以我配置了一个处理程序,如下所示:
I have a simple script using a ttk.Treeview
instance that I'm populating with the contents of a file system tree. I want to perform a certain operation when (leaf) items are clicked so I configured a handler like so:
self.tree.tag_bind('#entry', '<1>', self.onClick)
在 onClick
方法中,我只是打印出被点击的项目,如下所示:
In the method onClick
I am simply printing out the item that was clicked, like so:
def onClick(self, event):
item_id = str(self.tree.focus())
print 'Selected item was %s' % item_id
item = self.tree.item(item_id)
flag = '#another_tag' in item['tags']
print ' flag = %s' % flag
我发现消息比点击次数滞后一倍.所以我的第一次点击会得到一个随机值(看起来像树的根),然后第 n 次点击会打印出被点击的第 (n-1) 个项目的值.
I'm finding that the messages are lagging the clicks by one. So my first click gets a random value (looks like the root of the tree), and then the n-th click prints out the values for the (n-1)th item that was clicked.
它们是这样插入的:tree.insert(parent_id, 'end', id, text=id, tags=['#entry'])
有人知道这是 Tkinter 中的错误还是我做错了什么?
Anyone know if this is a bug in Tkinter or something that I'm doing wrong?
这似乎是 Ubuntu Natty 和 OS X Lion 上的一个问题(使用 Python 和 Tkinter 的默认预安装版本)
This appears to be an issue on both Ubuntu Natty as well as OS X Lion (using the default pre-installed versions of Python and Tkinter)
这就是 Tkinter 设计的工作方式.小部件上的绑定在小部件类上的绑定之前处理.设置所选项目的是小部件类上的绑定.这使得覆盖默认绑定变得非常容易,但代价是增加默认绑定变得更加困难.
This is the way Tkinter is designed to work. Bindings on a widget are processed before bindings on the widget class. It is the bindings on the widget class that set the selected item. This makes it really easy to override the default bindings, at the expense of making it slightly harder to augment default bindings.
这个网站已经被问过几次了.在本站搜索bindtags";bindtags 是控制事件处理顺序的机制.
This has been asked a few times on this site. Search for "bindtags" on this site; bindtags are the mechanism that controls the order of event processing.
在树视图小部件的特定情况下,我建议绑定到 <<TreeviewSelect>>
事件,该事件将在设置选择后处理.然后,您可以使用 tag_has
方法来确定单击了哪种节点.
In the specific case of the treeview widget, I recommend binding to the <<TreeviewSelect>>
event, which will be processed after the selection has been set. You can then use the tag_has
method to determine what sort of node was clicked on.
这篇关于为什么我的 ttk.Treeview 点击处理程序在 tree.focus() 上返回错误的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!