答案 0 :(得分:4)
Tkinter没有像wxPython的ChecklistBox这样的小部件。但是,在框架内创建一组检查按钮是很容易的。
示例:
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 = ("Author", "John", "Mohan", "James", "Ankur", "Robert")
checklist = ChecklistBox(root, choices, bd=1, relief="sunken", background="white")
...
print("choices:", checklist.getCheckedItems())