Signup/Sign In

Python Tkinter Scrollbar Widget

In this tutorial, we will cover the Tkinter Scrollbar widget in Python, using which we can add a scrollbar to the user interface of our Tkinter application.

To scroll up or down or right or left the content in a Python desktop application, the Tkinter Scrollbar widget is used.

  • To scroll the content of other widgets like Listbox, canvas, etc we use this widget.

  • Both Horizontal and Vertical scrollbars can be created in the Trinket Entry widget.

Below we have an image showing scrollbar widget used with a Listbox:

Tkinter scrollbar wdget example

Tkinter Scrollbar Widget

The syntax of the Scrollbar widget is given below:

W = Scrollbar(master, options)   

In the above syntax, the master parameter denotes the parent window. You can use many options to configure your scrollbar widget and these options are written as comma-separated key-value pairs.

Tkinter Scrollbar Widget Options:

Following are the various options used with Tkinter Scrollbar widgets:

Name of the Option Description
activebackground This option represents the background color of the widget when it is under focus.
bg This option represents the background color of the widget
bd This option represents the border size of the widget. The default value is 2 pixels.
cursor With the help of this option, the mouse pointer will be changed to a specific cursor type and it can be an arrow, dot, etc.
command This option will be set to the procedure associated which is called every time the scrollbar is moved.
elementborderwidth This option mainly represents the border width around the arrowheads and the slider. The default value of this option is -1.
highlightthickness This option represents the thickness of the focus highlights
highlightbackground This option indicates the highlight color when the widget is not under the focus
highlightcolor This option indicates the highlight color when the widget is under the focus
jump This option is used to control the behavior of the scroll jump. If this option is set to 1, then the callback is called at the time when the user releases the mouse button.
orient This option can be set to either horizontal or vertical depending upon the orientation of the scrollbar.
width This option represents the width of the scrollbar.
troughcolor This option is used to set the color for the trough
takefocus By default, you can tab the focus through this widget. If you don't want this behavior you can set this option to 0.
repeatdelay This option is mainly used to tell the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. its default value is 300 ms
repeatinterval The default value of this option is 100

Tkinter Scrollbar Widget Methods:

Few methods used with Tkinter Scrollbar widgets are:

  • get():

    This method returns the two numbers suppose a and b which represents the current position of the scrollbar.

  • set(first, last):

    This method is used to connect the scrollbar to any other widget. That is yscrollcommand or xscrollcommand of the other widget to this method.

Tkinter Scrollbar Widget Example

Below we have a basic example of a scrollbar widget.

from tkinter import *  
win= Tk()  
sbb = Scrollbar(win)  
sbb.pack(side = RIGHT, fill = Y)  
mylist = Listbox(win, yscrollcommand = sbb.set)  
for line in range(45):  
    mylist.insert(END, "Value " + str(line))  
mylist.pack(side = LEFT)
sbb.config(command = mylist.yview)

Tkinter Scrollbar Widget example

As you can see in the above code, we have created a Listbox widget with numbers as list items in it. Then we have created a Scrollbar widget and have used the yscrollcommand option of the Listbox widget to set the Scrollbar widget with it. We have used the Scrollbar widget's set function here.


So we have covered the Tkinter Scrollbar widget. It is very useful where we have a Tkinter application with long lists or some widget which is too long to fit in the application window. Then we can use the Scrollbar widget for such applications.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight