1

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?

  • Is the empty space as big as when there were those 100 buttons? Also how is frame1 not None, since .grid returns None, that is, how can you set it as, oh ok, split that line in two, line where you initialise frame and part where you grid it – Matiiss Jul 31 '21 at 13:45
  • Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name), the issue is the same except a bit different case here, those buttons are put in the main Tk window or sth since their master is None – Matiiss Jul 31 '21 at 13:48
  • @Matiiss Yes, the space created is as big as where the 100 buttons used to be. Basically I am making a sort of table using labels inside a frame, with a button to show more lines of the table. But when the user wants some other data, I need to delete all widgets in that frame, in order to create a new table in its place(again with labels). But now, there is additional space below the new table that makes it look unprofessional – Tirth Anand Aug 10 '21 at 05:23
  • I couldn't reproduce the issue, however your code has errors as I already mentioned `frame1 is None` for example, it has to throw an error about using incorrect managers, also when I destroyed the widgets all worked fine, please post the actual code (this doesn't work when copy pasted) – Matiiss Aug 10 '21 at 06:48

0 Answers0