我在查询(Python)ttk.Treeview
item
的open
选项时遇到了不好的行为。节点(item
)的可见性可以通过执行以下操作来设置:
tree.item(someItemID, open=True) # or
tree.item(someItemID, open=False)
我的假设是可以查询open
选项来获得布尔值True/False。然而,情况似乎并非如此。考虑一下这个脚本:
运行时,它显示一个小的Treeview控件,其中填充了假目录和文件名。在打开或关闭任何顶层节点之前,按下按钮将open
选项状态转储到stdout。应该是这样的:
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)
现在打开其中一个节点并再次按下按钮。立即转储:
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)
最后,关闭所有节点并再次按下按钮。它倾倒:
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)
对我来说最突出的是:
int
s时,稍后分配的值是_tkinter
对象_tkinter
对象呈现为字符串“true”或“false”,但它们的求值结果不为true和false(例如,_tkinter
对象打印为“false”并计算为true)有人知道什么是给予吗?如何可靠地确定Treeview项的打开/关闭状态?在
有一个tkinter选项:
tkinter.wantObjects
,有些人愿意将其改为False。它应该使Tk不使用TCL_objs。但当我试过的时候,树景看起来很破碎。在作为一种解决方法,我用如下方式使用BooleanVar:
我觉得这种方法很管用
在Barron Stone的Lynda课程中,用Tkinter开发PythonGUI,这是一个教学视频Building a hierarchical treeview,其中有一个示例演示了如何获得“is open?”结果。我修改了以下示例:
IDLE控制台中的python3.5
不是请求的布尔值,而是一个
int
也一样好。在相关问题 更多 >
编程相关推荐