首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将tkinter checkboxtreeview的默认值设置为“选中”

如何将tkinter checkboxtreeview的默认值设置为“选中”
EN

Stack Overflow用户
提问于 2022-11-14 14:08:35
回答 2查看 32关注 0票数 0

我用ttkwidget创建了一个复选框treeview。默认情况下,我希望这些复选框被“选中”。我怎么能这么做。

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2022-11-14 14:47:21

查看CheckboxTreeview小部件这里这里源代码,我发现了以下change_state方法

代码语言:javascript
复制
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项设置检查状态的预定方式,因此您应该能够这样做

代码语言:javascript
复制
tree.change_state(iid, 'checked')  # where 'iid' is the item id you want to modify
票数 0
EN

Stack Overflow用户

发布于 2022-11-16 07:48:47

根据CheckboxTreeview的源代码,您可以将tags=("checked",)设置为在调用.insert(...)时首先选中复选框。

代码语言:javascript
复制
list = ["apple", "banana", "orange"]

for n, x in enumerate(list):
    tree.insert(parent="", index="end", iid=n,  text=x, tags=("checked",))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74432955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档