检索 ttk.Treeview 项目的“打开"选项作为布尔值 [英] Retrieving ttk.Treeview item's 'open' option as boolean

查看:20
本文介绍了检索 ttk.Treeview 项目的“打开"选项作为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查询 (Python) ttk.Treeview itemopen 选项时遇到了不良行为.可以通过执行以下操作来设置节点 (item) 的可见性:

I encounter untoward behavior when querying the open option of a (Python) ttk.Treeview item. The visibility of a node (item) can be set by doing something like:

tree.item(someItemID, open=True) # or
tree.item(someItemID, open=False) 

我的假设是可以查询 open 选项以获得布尔值 True/False.然而,情况似乎并非如此.考虑这个脚本:

And my assumption is the open option can be queried to get a boolean True/False. However, this doesn't appear to be the case. Consider this script:

from Tkinter import *
from ttk import Treeview

def check_state():
    for row in tree.get_children():
        opened = tree.item(row, option='open')
        print row, 'opened:', opened, '(type: %s)' % str(type(opened)), 'Got:',
        if not opened:
            print 'False (bool)'
        elif opened == 'true':
            print 'equal to string "true"'
        elif opened == 'false':
            print 'equal to string "false"'
        elif opened:
            print 'True (bool)'
        else:
            print 'something entirely different(!)'
    print

win = Frame()
tree = Treeview(win)
win.pack()
tree.pack()
Button(win, text='View state', command=check_state).pack()


level1 = ['C:\\dir1', 'C:\\dir2', 'C:\\dir3']
level2 = ['one.txt', 'two.txt', 'three.txt']
for L in level1:
    iid = tree.insert('', END, text=L)
    for M in level2:
        tree.insert(iid, END, text=M)

win.mainloop()

运行时,它会显示一个填充有假目录和文件名的小型 Treeview 控件.打开或关闭任何顶级节点之前,按下按钮将open选项状态转储到标准输出.应该是这样的:

When run, it displays a small Treeview control populated with fake directories and file names. Before opening or closing any of the top-level nodes, press the button to dump the open option states to stdout. Should look like this:

I001 opened: 0 (type: <type 'int'>) Got: False (bool)
I005 opened: 0 (type: <type 'int'>) Got: False (bool)
I009 opened: 0 (type: <type 'int'>) Got: False (bool)

现在打开其中一个节点并再次按下按钮.现在它转储了:

Now open one of the nodes and press the button again. Now it dumps:

I001 opened: 0 (type: <type 'int'>) Got: False (bool)
I005 opened: 0 (type: <type 'int'>) Got: False (bool)
I009 opened: true (type: <type '_tkinter.Tcl_Obj'>) Got: True (bool)

最后,关闭所有节点并再次按下按钮.它转储:

Finally, close all nodes and press the button once more. It dumps:

I001 opened: 0 (type: <type 'int'>) Got: False (bool)
I005 opened: 0 (type: <type 'int'>) Got: False (bool)
I009 opened: false (type: <type '_tkinter.Tcl_Obj'>) Got: True (bool)

让我印象深刻的事情:

  1. 不一致:虽然初始化为ints,但后来赋值的是_tkinter对象
  2. 布尔比较失败:尽管 _tkinter 对象呈现为字符串true"或false",但它们不会计算为 True 和 False(例如打印的 _tkinter 对象作为假"评估为真)
  1. Inconsistency: while initialized to ints, later the values assigned are _tkinter objects
  2. Boolean comparison failure: despite the _tkinter objects rendering as the strings 'true' or 'false' they do not evaluate to True and False (e.g. the _tkinter object printed as "false" evaluated as True)

有人知道是什么吗?如何可靠地确定 Treeview 项的打开/关闭状态?

Anyone know what gives? How can I reliably determine the open/closed state of a Treeview item?

推荐答案

有一个 tkinter 选项:tkinter.wantObjects,有些人提议将其更改为 False.它应该使 Tk 不使用 TCL_objs.但是当我尝试它时,TreeView 看起来坏了.

There was a tkinter option: tkinter.wantObjects that some people offered to change to False. It should make Tk to do not use TCL_objs. But when I tried it the TreeView looked broken.

作为一种解决方法,我以如下方式使用 BooleanVar:

As a workaround I used BooleanVar in way like this:

open_opt = BooleanVar()
for row in tree.get_children():
    open_opt.set(str(tree.item(row, option='open')))
    opened = open_opt.get()

这种方式对我来说似乎有效

This way seemed working to me

这篇关于检索 ttk.Treeview 项目的“打开"选项作为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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