【发布时间】:2013-03-26 23:30:57
【问题描述】:
问题很简单,我在 Text 小部件中使用 window_create 创建了许多复选框。代码如下:
import tkinter as tk
root = tk.Tk()
sb = tk.Scrollbar(orient="vertical")
text = tk.Text(root, width=40, height=20, yscrollcommand=sb.set)
sb.config(command=text.yview)
sb.pack(side="right",fill="y")
text.pack(side="top",fill="both",expand=True)
for i in range(30):
cb = tk.Checkbutton(text="checkbutton %s" % i,padx=0,pady=0,bd=0)
text.window_create("end", window=cb)
text.insert("end", "\n")
root.mainloop()
这是它的样子:
我想选择多个复选框,如果我必须单击每个复选框,这很麻烦。那么有没有一种方法可以在这里使用SHIFT?
【问题讨论】:
-
您是否考虑过添加“全选”和“取消全选”按钮来做同样的事情,而不是绑定?大多数用户永远不会意识到他们可以通过 shift-click 来全选,而且您不能依赖他们阅读用户指南。
-
是的,添加“全选”和“反向选择”按钮是我的计划。但有时使用“移位选择”会更好,例如您想选择几块复选框。
-
@BryanOakley 你能看一下选择的答案吗?如果将
for cb in self.chkbuttons[sl]: cb.toggle()更改为map(tk.Checkbutton.toggle, self.chkbuttons[self.start+1:end])它将不起作用,我不知道为什么。谢谢!