How to add scrollbar in Tkinter – Python
In this tutorial, we will explore how to add a scrollbar to your Python applications built with the Tkinter framework.
Scroll bar widget in Tkinter
In my earlier tutorial, I explained how to add a horizontal scrollbar in Tkinter to your Python applications. Since you already know how to add a scrollbar horizontally, you have to change the orientation, axis, and a couple of parameters in your text widget to implement a vertical scrollbar. Let’s see what parameters require the change.
- Firstly, you have to change the
wrap="word"
parameter. If you keep it to “none,” it will not print in new lines. - Now, you have to change the orientation and axis of the scrollbar widget. Keep the
orient = "vertical"
and change the view to.yview
. Also, change the fill parameter tofill= "y"
.
import tkinter as tk root = tk.Tk() root.title("Vertical Scrollbar Example") text_widget = tk.Text(root, wrap="word") text_widget.pack(side="left", fill="both", expand=True) v_scroll = tk.Scrollbar(root, orient="vertical", command=text_widget.yview) v_scroll.pack(side="right", fill="y") text_widget.config(yscrollcommand=v_scroll.set) for i in range(50): text_widget.insert("end", "Row " + str(i) + "\n") root.mainloop()
Leave a Reply