Reputation: 18953
Why doesn't the scrollbar initiate when create_window
frame objects start to exceed the bottom self.container
window?
My understanding is that widgets are scrollable if they are embedded on the canvas using create_window
. For context, I don't want to create a scrolling frame - put all your widget in a frame, use create_window
to add that frame to the canvas - because I intend to move these frame objects around on the canvas and leverage a lot of canvas capabilities. According to Effbot, You cannot draw other canvas items on top of a widget., so if I had a scrolling frame, I wouldn't be able to put widgets on top of that.
So how do I scroll the canvas that contains many create_window
objects, or, what am I doing wrong below?
import tkinter as tk
class Canvas_Scrollbar_CreateWindow(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.parent.columnconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.block_count = 0
self.button = tk.Button(self, text='Add', command=self.addblock)
self.button.grid(row=0, column=0, columnspan=2, sticky='new')
self.container = tk.Frame(self)
self.container.grid(row=1, column=0, sticky='nsew')
self.canvas = tk.Canvas(self.container, width=200, height=450)
self.scrollbar = tk.Scrollbar(self.container,
orient='vertical',command=self.canvas.yview)
self.canvas.config(yscrollcommand=self.scrollbar.set)
self.canvas.grid(row=0, column=0, sticky='nsew')
self.scrollbar.grid(row=0, column=1, sticky='nse')
self.container.bind('<Configure>', self.handle_scroll)
def addblock(self):
self.block = tk.Frame(self.canvas, bd=1, relief='solid')
self.block.columnconfigure(0, weight=1)
self.canvas.create_window((0, (self.block_count*25)),
window=self.block, anchor="nw",
width=200, height=24)
self.block_count += 1
def handle_scroll(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
root = tk.Tk()
app = Canvas_Scrollbar_CreateWindow(root)
app.grid(row=0, column=0, sticky='ew')
root.mainloop()
Upvotes: 0
Views: 1387
Reputation: 386255
You must re-configure scrollregion
when you add items to the canvas.
Upvotes: 2