Tkinter 支持相对无限数量的复选框,主要受系统内存和可用性限制等实际问题的限制。
至少有三种技术可以为小部件制作可滚动容器。画布和文本小部件都支持滚动,因此普遍接受的做法是将其中一种用于容器。如果您需要一些复杂的东西,您还可以使用 place 命令做一些巧妙的技巧。
如果您想滚动包含多个对象的垂直列表以外的框架,则使用画布是很好的选择。如果您只需要创建一个垂直列表,则使用文本小部件非常方便。
这是一个简单的例子:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, root, *args, **kwargs):
tk.Frame.__init__(self, root, *args, **kwargs)
self.root = root
self.vsb = tk.Scrollbar(self, orient="vertical")
self.text = tk.Text(self, width=40, height=20,
yscrollcommand=self.vsb.set)
self.vsb.config(command=self.text.yview)
self.vsb.pack(side="right", fill="y")
self.text.pack(side="left", fill="both", expand=True)
for i in range(1000):
cb = tk.Checkbutton(self, text="checkbutton #%s" % i)
self.text.window_create("end", window=cb)
self.text.insert("end", "\n") # to force one checkbox per line
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
当您了解更多有关 Tkinter 的信息时,您会意识到内置小部件并不像其他一些工具包那么多。希望你也会意识到 Tkinter 有足够的基本构建块来做任何你能想象到的事情。