Assume a small Tkinter window with two frames, one having 20 buttons vertically and the other having 100 buttons vertically. These frames be one below the other. The window is small and so I create a scrollbar like this:
from tkinter import*
root = Tk()
root.geometry('200x100')
main_frame = Frame(root)
main_frame.pack(fill = BOTH, expand = 1)
main_canvas = Canvas(main_frame, bg = 'snow')
main_canvas.pack(side = LEFT, fill = BOTH, expand = 1)
vertical_scrollbar = ttk.Scrollbar(main_frame, orient = VERTICAL, command = main_canvas.yview)
vertical_scrollbar.pack(side = RIGHT, fill = Y)
main_canvas.configure(yscrollcommand = vertical_scrollbar.set)
main_canvas.bind('<Configure>', lambda e: main_canvas.configure(scrollregion = main_canvas.bbox("all")))
screen_frame = Frame(main_canvas, bg = 'snow')
main_canvas.create_window((0,0), window = screen_frame, anchor = 'nw')
frame1 = Frame(screen_frame, bg = 'snow').grid(row = 0, column = 0)
frame2 = Frame(screen_frame, bg = 'snow').grid(row = 1, column = 1)
for i in range(0, 20):
Button(frame1, text = 'Button').grid(row = i, column = 0)
for j in range(0, 100):
Button(frame2, text = 'Button').grid(row = j, column = 0)
root.mainloop()
So at this point, we will be able to scroll till the end of the second frame. But Now if I do the following:
for widgets in frame2.winfo_children():
widgets.destroy()
Frame 2 becomes empty. But now the scrollbar doesn't end at the end of frame1. Rather there is an empty space below frame1 where 100 buttons of frame2 used to be.
So my doubt is, how do we remove this empty space?