Scrollbar in a Text Widget in Tkinter with Python

The scrollbar in a text widget appears when a text goes beyond the height of the text, but sometimes it may not appear for some reason. It happened to me, perhaps because of the fact that the text was in a frame and for some configuration of the widget. Anyway, I solved the fact that the scrollbars did not appear, adding them with the following code:

from tkinter import *
root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
textbox = Text(root)
textbox.pack()
for i in range(100):
    textbox.insert(END, f"This is an example line {i}\n")
# attach textbox to scrollbar
textbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=textbox.yview)
 
root.mainloop()

So, you should not need to add scrollbar to a text widget (while in the listbox you have to explicit add this code to add the scrollbars), but in case you do not see them for some reason, use this code.

Tkinter test for students

Tkinter articles

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.