我编写了一个类,它基本上管理可滚动框架内的一组Checkbuttons:
import tkinter as tk
from tkinter import ttk
class SelectionList(ttk.Frame):
def __init__(self, master, *args, **kwargs):
ttk.Frame.__init__(self, master, *args, **kwargs)
vscrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL)
vscrollbar.pack(fill=tk.Y, side=tk.RIGHT, expand=tk.FALSE)
canvas = tk.Canvas(self, bd=0, highlightthickness=0,
yscrollcommand=vscrollbar.set)
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.TRUE)
vscrollbar.config(command=canvas.yview)
canvas.xview_moveto(0)
canvas.yview_moveto(0)
self.__interior = ttk.Frame(canvas)
self.__interior.pack()
canvas.create_window(0, 0, window=self.__interior, anchor=tk.NW)
def add_item(self, item):
cb = ttk.Checkbutton(self.__interior, text=str(item))
cb.pack(fill=tk.X, padx=5, pady=2)
在大多数地方,这个小部件按照我的预期方式工作。但是,当我将一个SelectionList添加到ttk.Notebook然后调用add_item方法时,我得到以下错误,我无法弄清楚:
_tkinter.TclError: bad window path name ".!application.[...].!notebook.!selectionlist.!canvas.!frame"