【发布时间】:2019-02-11 20:36:19
【问题描述】:
使用 Ttk Treeview 控件时,我在尝试以编程方式设置多个选定项时收到 TclError。
在 Treeview 控件中设置多个项目的正确方法是什么?
The documentation 不清楚items 允许哪些类型:
selection_set(items)items 成为新的选择。
我已将代码简化为以下内容:
try: # python 2
import Tkinter as tk
import ttk
except ImportError: # python 3
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root)
for text in ['apple', 'banana', 'coconut']:
tree.insert('', 'end', text=text)
all_items = list(tree.get_children())
print("all_items = {!r}".format(all_items))
tree.selection_set(all_items)
但是,它引发了一个异常:
all_items = ['I001', 'I002', 'I003']
Traceback (most recent call last):
File ...
tree.selection_set(all_items)
File "C:\Python27\lib\lib-tk\ttk.py", line 1402, in selection_set
self.selection("set", items)
File "C:\Python27\lib\lib-tk\ttk.py", line 1397, in selection
return self.tk.call(self._w, "selection", selop, items)
_tkinter.TclError: Item ['I001', not found
错误消息的最后一行看起来像是仅使用 str 将项目列表转换为字符串,但格式不是后端所期望的。
【问题讨论】:
标签: python python-2.7 tkinter ttk