This code creates a window with a text box and a vertical scroll bar. If the window's width is changed by dragging the edge of the window, and the width decreases below some threshold (~660 pixels) the vertical scrollbar disappears.
What is causing the vertical scroll bar to be pushed out of the window when this threshold is reached? I've not defined widths for any widgets here. I would expect the text box to just get smaller and smaller as the window's width is reduced.
import tkinter
window = tkinter.Tk()
text_box = tkinter.Text(master = window)
text_box.pack(side = "left", fill = "both", expand = True)
vertical_scrollbar = tkinter.Scrollbar(master = window, command = text_box.yview)
vertical_scrollbar.pack(side = "right", fill = "y")
window.mainloop()
A simple solution is to pack the scrollbar first, and then the text widget. pack
has what's called a packing order. When it lays out the widgets it traverses the widgets in the order that they are packed, allocating the requested space for each widget. When it runs out of space, any additional widgets won't be visible.
Because you didn't specify a size for the text widget, the width defaults to 24 characters. When the window containing the text widget shrinks, the packer has to shrink or remove widgets so that everything will fit. When it does this it starts from the last widget in the packing order. In your original example this is the scrollbar, which explains why it disappears.
Packing the scrollbar first moves the text widget to the end of the packing order, so when things don't fit the packer will start to shrink the text widget first.
import tkinter
window = tkinter.Tk()
text_box = tkinter.Text(master = window)
vertical_scrollbar = tkinter.Scrollbar(master = window, command = text_box.yview)
vertical_scrollbar.pack(side = "right", fill = "y")
text_box.pack(side = "left", fill = "both", expand = True)
window.mainloop()
For the definitive description of how pack
works, see The Packer Algorithm in the tcl/tk documentation.