我知道这个问题的某种形式已经在这里以及其他论坛上被询问过。我已经阅读了所有这些解决方案,并为这些解决方案努力了几个小时,但我仍然无法得到它。目标只是一个新的窗口(Toplevel),里面有一个可滚动的画布,里面有一些内容。我仍然无法让画布上的滚动条工作:Tkinter上的滚动条上的滚动条
#make new window
self.edit_window = Tkinter.Toplevel()
self.edit_window.title("Data Refinement")
self.edit_window.maxsize(height='50', width='300')
#make scrollbar for canvas
cScrollbar = Tkinter.Scrollbar(self.edit_window)
cScrollbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y)
#make canvas
canvas = Tkinter.Canvas(self.edit_window)
#attach canvas to scrollbar
canvas.config(yscrollcommand=cScrollbar.set)
cScrollbar.config(command=canvas.yview)
#make frame and put everything in frame
frame = Tkinter.Frame(self.edit_window)
#random fill
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
Tkinter.Label(frame, text="Enter bounds for the parameters").pack()
#scrollbar for listbox
scrollbar = Tkinter.Scrollbar(frame)
scrollbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y)
listbox = Tkinter.Listbox(frame)
listbox.pack()
#insert some random data for now into listbox
for i in range(100):
listbox.insert(Tkconstants.END, i)
# attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
#pack and attach to canvas
frame.pack(fill=Tkconstants.BOTH, expand=Tkconstants.YES)
canvas.create_window(0,0, anchor = Tkconstants.NW, window = frame)
canvas.pack(fill=Tkconstants.BOTH, expand=Tkconstants.YES)
canvas.config(scrollregion=canvas.bbox(Tkconstants.ALL))
因此,框架已成功制作内容。框架已成功连接到画布。我没有得到的是附加到列表框的滚动条工作,而连接到画布的滚动条显示,但实际上并没有工作。滚动条的作用就像已经显示的一切。这就像滚动可见内容,而不是滚动画布的全部内容。
有没有人看到为什么我这样做的方式适用于列表框但不是画布?我一直在线应用滚动画布的例子,但我得到同样的问题....这是非常可怕的 – user926914