下面是使用Tkinter在列表框中定义滚动条的代码:
import tkinter as tk
root = tk.Tk()
scrollbar = tk.Scrollbar(root)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
listbox = tk.Listbox(root)
listbox.pack()
for i in range(50):
listbox.insert(tk.END, i)
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=s.set)
root.mainloop()
我想修改此代码以使滚动条带有复选按钮,我知道我无法用复选按钮填充列表框,因为列表框只能包含文本
scrollbar = tk.Scrollbar(self)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
categories = ["aaa","bbb","ccc"]
for i in categories:
var[i] = tk.IntVar()
chk = tk.Checkbutton(self, text=i, variable=var[i], width=20)
#chk.select()
chk.pack()
scrollbar.config(command=s.set)
如何使我的Checkbuttons“可滚动”?
答案 0 :(得分:1)
最常见的选择是将检查按钮放在框架中,然后将框架放入画布中,因为画布是可滚动的。有很多例子,包括这个问题:Adding a scrollbar to a group of widgets in Tkinter
在处理垂直小部件堆栈的情况下,一种简单的解决方案是将复选按钮嵌入文本小部件中,因为文本小部件既支持嵌入式小部件又支持垂直滚动。
示例:
import tkinter as tk
root = tk.Tk()
scrollbar = tk.Scrollbar(root)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
checklist = tk.Text(root, width=20)
checklist.pack()
vars = []
for i in range(50):
var = tk.IntVar()
vars.append(var)
checkbutton = tk.Checkbutton(checklist, text=i, variable=var)
checklist.window_create("end", window=checkbutton)
checklist.insert("end", "\n")
checklist.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=checklist.yview)
# disable the widget so users can't insert text into it
checklist.configure(state="disabled")
root.mainloop()