我正在使用来自this answer的代码创建一个带有复选框的列表
import tkinter as tk
root = tk.Tk()
class ChecklistBox(tk.Frame):
def __init__(self, parent, choices, **kwargs):
tk.Frame.__init__(self, parent, **kwargs)
self.vars = []
bg = self.cget("background")
for choice in choices:
var = tk.StringVar(value=choice)
self.vars.append(var)
cb = tk.Checkbutton(self, var=var, text=choice,
onvalue=choice, offvalue="",
anchor="w", width=20, background=bg,
relief="flat", highlightthickness=0
)
cb.pack(side="top", fill="x", anchor="w")
def getCheckedItems(self):
values = []
for var in self.vars:
value = var.get()
if value:
values.append(value)
return values
choices = [str(e) for e in range(100)]
checklist = ChecklistBox(root, choices, bd=1, relief="sunken", background="white")
checklist.pack()
由于选择列表很长,我想在列表中添加一个滚动条。最好的方法是什么
我试图遵循示例here,但是ChecklistBox
没有yview
方法,也没有yscrollcommand
选项。我不知道如何避开这个问题
我也有类似的需求,并拼凑了这个控件小部件复选框,它是基于大量的web示例。如果我知道我会分享这段代码,我会保留这些参考资料。 注意:我是拼凑起来的,并没有像ScrolledWindow类那样创建主要部分
主要特点,, 检查[0,1,-1] 0未选中,1已选中,-1未选中图像 选项项目图像
有关项目的应用程序数据: 任何像tooltext这样的id文本
CheckListBox Demo Image
问题的根源在于帧不可滚动。因此,您必须找到一个支持滚动的小部件,并将其作为向一组小部件添加滚动的基础
Canvas
小部件通常用于此目的。它通常与内部框架结合使用,这使得使用pack
或grid
来排列小部件变得很容易。但是,因为您要创建一个相同小部件的垂直堆栈,所以直接在画布上绘制复选按钮更容易第一步是向框架添加画布和滚动条:
接下来,我们将调用
create_window
,而不是在checkbutton上调用pack
。我们可以得到上一个项目的y坐标,以确定下一个项目的放置位置。我们将使用帧的pady
选项作为间距相关问题 更多 >
编程相关推荐