我用ttkwidget创建了一个复选框treeview。默认情况下,我希望这些复选框被“选中”。我怎么能这么做。
from tkinter import *
from ttkwidgets import CheckboxTreeview
import tkinter as tk
root = tk.Tk()
tree = CheckboxTreeview(root)
tree.pack()
list = [("apple"), ("banana"), ("orange")]
n=0
for x in list:
tree.insert(parent="", index="end", iid=n, text=x)
n+=1
root.mainloop()发布于 2022-11-14 14:47:21
查看CheckboxTreeview小部件这里的这里源代码,我发现了以下change_state方法
def change_state(self, item, state):
"""
Replace the current state of the item.
i.e. replace the current state tag but keeps the other tags.
:param item: item id
:type item: str
:param state: "checked", "unchecked" or "tristate": new state of the item
:type state: str
"""
tags = self.item(item, "tags")
states = ("checked", "unchecked", "tristate")
new_tags = [t for t in tags if t not in states]
new_tags.append(state)
self.item(item, tags=tuple(new_tags))这似乎是为treeview项设置检查状态的预定方式,因此您应该能够这样做
tree.change_state(iid, 'checked') # where 'iid' is the item id you want to modify发布于 2022-11-16 07:48:47
根据CheckboxTreeview的源代码,您可以将tags=("checked",)设置为在调用.insert(...)时首先选中复选框。
list = ["apple", "banana", "orange"]
for n, x in enumerate(list):
tree.insert(parent="", index="end", iid=n, text=x, tags=("checked",))https://stackoverflow.com/questions/74432955
复制相似问题