Python has several options for constructing a graphical user interface. Tkinter is the most widely used GUI technique out of all the options. It’s a Python language interface to the Python-provided Tk GUI toolkit.

A Scrollbar is a Python Tkinter widget that provides continuous data to be scrolled in a predetermined direction on the screen. The direction can either be horizontal or vertical. Scrollbars are inserted when the content surpasses the screen orientation, allowing the user to travel to unseen material in either a right-to-left or upward-to-downward direction.

Layout managers, such as Place(), Grid(), and Pack() are used to position the Tkinter widgets in Python. The scrollbar widget can be placed on the application window using these methods.

Tkinter Scrollbar Syntax

The slide controller of the Tkinter Scrollbar widget is often used by programmers to make vertical scrolling widgets, such as Text and Canvas. You may also use the Tkinter Scrollbar widget to make a horizontal scrollbar for the entry widgets. Use the following Scrollbar() syntax to get a scrollbar with the master and option/options properties:

w=scrollbar( master, option/options, …  )

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Scrollbar-1.png" data-lazy- height="47" src="data:image/svg xml,” width=”494″>

This Tkinter Scrollbar’s master attribute solely represents the parent window. The Tkinter scrollbar widget’s option/options feature will contain a list of frequently used scrollbar widget options. These option/options are primarily used as key-value pairs, with commas separating them.

Tkinter Scrollbar Methods

The Tkinter Scrollbar Objects have three methods: get(), set (first1, last1), and Pack().

  • get(): Python’s get() method returns two values, “a” and “b”, which reflect the current position of the slider. The get() value specifies the exact position of the slider’s edge (left or right), as well as the vertical and horizontal scrollbars, whereas the “b” value specifies the position of the right or bottom edge.
  • set(first, last): The set() method is used to link the scroll bar/slider to another widget called “w”. Remember set() the yscrollcommand of w or the yscrollcommand of the “y”. These parameters are the same because of the values returned by the get() function.
  • Pack(): The alignment of the slider/sidebar can be set using this way.

Example 1:

Text widgets are among the many types of scrollable widgets. The following program shows a simple user interface of Text and Scrollbar widgets. We used “ttk” to construct a scrollbar in the code: a scrolling bar (orient, command). Orientation can be either “vertical” or “horizontal”. The scrollable widget’s yview or xview attribute corresponding to the scrollbar can be used as the command. Set the scrollable widget’s yscrollcommand attribute to link to the scrollbar:

import tkinter as tk

from tkinter import ttk


root_t = tk.Tk()


root_t.resizable(False, False)


root_t.title(“An Example of Scrollbar Widget”)


root_t.grid_columnconfigure(0, weight=1)


root_t.grid_rowconfigure(0, weight=1)


text = tk.Text(root_t, height=8)


text.grid(row=0, column=0, sticky=‘ew’)


scrollbar = ttk.Scrollbar(root_t, orient=‘vertical’, command=text.yview)


scrollbar.grid(row=0, column=1, sticky=‘ns’)


text[‘yscrollcommand’] = scrollbar.set


root_t.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Scrollbar-2.png" data-lazy- height="294" src="data:image/svg xml,” width=”559″>

Below is what the result looks like upon executing the code:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Scrollbar-3.png" data-lazy- height="161" src="data:image/svg xml,” width=”659″>

Example 2:

In Python Tkinter, a scrollbar can be applied to a Frame, allowing the user to scroll horizontally or vertically in the direction of the screen. It’s not always possible to easily view all of the words on a screen. As this will make the material less readable, and it will also look unattractive on the screen. Scrollbars were created to address this issue. Users can now scroll the scrollbar to access the large text content.

The best way is to add a scrollbar to the Frame. Put the scrollbar and the other widgets into the frame window and pack them in different ways. For example, put the scrollbars on the right side and the other widgets on the left side.

In Python Tkinter, scrollbars are performed on the following Frame widget. The complete code for implementing a scrollbar on a frame in Python Tkinter is shown below:

We’ve added a scrollbar and a Text box widget to the frame using Python Tkinter. We then used Pack layout manager to move the scrollbar to the right and the Textbox to the left. Finally, we’ve connected the scrollbar and the textbox:

from tkinter import *


ws1 = Tk()


ws1.title(‘Second Example of Scrollbar Widget’)


ws1.geometry(‘400×400’)


ws1.config(bg=‘#7FFFD4’)


frame = Frame(


    ws1,


    bg=‘#FF0000’


    )


text1_box = Text(


    ws1,


    height=15,


    width=30,


    font=(14)  

)


text1_box.pack(side=LEFT,expand=True)


text1_box.config(bg=‘#F0F8FF’)


sb_ver = Scrollbar(


    ws1,


    orient=VERTICAL


    )


sb_ver.pack(side=RIGHT, fill=Y)


text1_box.config(yscrollcommand=sb_ver.set)


sb_ver.config(command=text1_box.yview)


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Scrollbar-4.png" data-lazy- height="490" src="data:image/svg xml,” width=”552″>

The output of the explained code is provided below. The scrollbar is highlighted on the right in this output. By scrolling the scrollbar, the content put in the text field can be easily viewed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Scrollbar-5.png" data-lazy- height="430" src="data:image/svg xml,” width=”399″>

Example 3:

Here’s an example of a Grid-based Scrollbar. Grid is a Layout manager in Python Tkinter that arranges widgets in a row and column format. The x-axis represents the rows, while the y-axis represents the columns. We’ll add a scrollbar to the Text widget using Python Tkinter’s Grid Layout Manager.

The Text widget and the Scrollbar widget will be placed at row=0 and column=0 for the Text widget and column=1 for the Scrollbar widget, according to the approach. Both widgets will display parallel to each other in this manner. We can stretch the scrollbar in a north-south direction by using sticky.

Finally, we’ll connect the scrollbar and the text box widget. Please note that any widget can be used in place of the text widget. We’ve established a text editor also with a Text box widget in this code, and the scroll bar will appear if the words exceed the Text widget’s orientation.

from tkinter import *


ws1 = Tk()


ws1.title(‘Third Example of Tkinter Scrollbar’)


ws1.config(bg=‘#7FFFD4’)


frame = Frame(


    ws1,


    bg=‘#FF0000’


    )


text1_box = Text(


    ws1,


    height=12,


    width=39,


    font=(14)  

)


text1_box.grid(row=0, column=0)


text1_box.config(bg=‘#F0F8FF’)


sb = Scrollbar(


    ws1,


    orient=VERTICAL


    )


sb.grid(row=0, column=1, sticky=NS)


text1_box.config(yscrollcommand=sb.set)


sb.config(command=text1_box.yview)


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Scrollbar-6.png" data-lazy- height="480" src="data:image/svg xml,” width=”548″>

You can notice that the scrollbar appeared as soon as the words began to exceed the Text box size in this output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Scrollbar-7.png" data-lazy- height="246" src="data:image/svg xml,” width=”369″>

Conclusion:

This is a tutorial for the Tkinter Scrollbar. When we have a Tkinter application containing long lists or a widget that is too big to fit in the application window, this is quite beneficial. The Scrollbar widget can be used for such applications. We’ve gone over the basics of the Tkinter Scrollbar widget, its features, and several examples to demonstrate how it works.We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content