Python Tkinter Scrollbar – How to use

In this Python tkinter tutorial, we will learn about Python Tkinter Scrollbar. Also, we will cover these topics:

  • Python Tkinter Scrollbar
  • Python Tkinter Scrollbar Grid
  • Python Tkinter Scrollbar Frame
  • Python Tkinter Scrollbar Canvas
  • Python Tkinter Scrollbar Text box
  • Python Tkinter Scrollbar Not Working
  • Python Tkinter Scrollbar Text Widget
  • Python Tkinter Scrollbar Orient

Python Tkinter Scrollbar

A Scrollbar in Python Tkinter is a widget in which continuous text, pictures, or any other multimedia content can be scrolled in a predetermined direction (Horizontal or Vertical) on the computer window screen.

  • When the content exceeds the screen orientation in that case scrollbars are added so that user can navigate to unseen content either in the right-left or upward-downward direction.
  • In Python Tkinter widgets are positioned using layout managers like Place(), Grid() and Pack(). We can use these layout managers to position the scrollbar widget on the application window.

Syntax of Scrollbar in Python Tkinter

  • In this syntax, ws is the main screen on which we want to place the scrollbar. Orient determines whether Scrollbar will be Vertical or Horizontal.
  • The layout manager we are using is Pack().
  • Once we have successfully created the scrollbar widget now it’s time to apply it on a widget. It is an important step as it binds the scrollbar with the specific widget in the application.
  • For example, In the email app, we see the scrollbar only in the text area that too when the content exceeds the orientation of the Text widget.
  • While binding the scrollbar with other applications we specify the scroll direction by passing X or Y in scollcommand. code at lines 11, 12 shows the binding code.
# creating and placing scrollbar
sb = Scrollbar(
    ws,
    orient=VERTICAL
)
sb.pack()


# binding scrollbar with other widget (Text, Listbox, Frame, etc)

<other_widget>.config(yscrollcommand=sb.set)
sb.config(command=<other_widget>.yview)

Here is the preview of Scrollbar in Python Tkinter. The picture shows the Vertical scrollbar.

python tkinter scrollbar on frame
Scrollbar in Python Tkinter

Read: Python tkinter label – How to use

Python Tkinter Scrollbar Grid

Grid in Python Tkinter is a Layout manager which Organizes the widgets in a row and columns format. Everything on the x-axis is the rows and everything on the y-axis is columns.

  • In this section, we will learn how to add a scrollbar on the Text widget using Grid Layout Manager in Python Tkinter.
  • The strategy is we will place the Text widget & Scrollbar widget at the row=0 and column=0 for Text widget and column=1 for the Scrollbar widget. In this way, both the widgets will appear parallel to each other (line 19, 27).
  • By using sticky we will stretch the scrollbar in the North-South direction (line 27).
  • In the end, we’ll bind the scrollbar with the text box widget (line 29, 30)
  • Note: Text widget can be replaced by any widget.

Here is the implementation of Scrollbar using Grid

In this code, we have created a text editor using Text box widget and if the number of words exceeds the orientation of the Text widget then scroll bar will appear.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.config(bg='#5F734C')

frame = Frame(
    ws,
    bg='#A8B9BF'
    )

text_box = Text(
    ws,
    height=13,
    width=32, 
    font=(12)  
)

text_box.grid(row=0, column=0)
text_box.config(bg='#D9D8D7')

sb = Scrollbar(
    ws,
    orient=VERTICAL
    )

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

text_box.config(yscrollcommand=sb.set)
sb.config(command=text_box.yview)


ws.mainloop()

Here is the output of the above code

READ:  modulenotfounderror: no module named 'matplotlib'

In this output, you can see that the scrollbar started appearing immediately after the words started exceeding the Text box size.

python tkinter scrollbars using grid
Python Tkinter Scrollbar

Read: Python Tkinter Entry – How to use

Python Tkinter Scrollbar Frame

Scrollbar in Python Tkinter can be applied on Frame which will help the user to scroll either horizontally or vertically direction of the screen.

  • It is not always possible to put in all the text on the screen. As that will hamper the readability of the content also it will ugly on the screen.
  • To solve this problem, scrollbar were introduced. Now users can access the huge text information simply by scrolling the scrollbar.
  • Applying scrollbar on the Frame is the easiest and the best way of doing. Simply put both (scrollbar & other widgets) inside the frame window and then pack them in opposite directions.
  • For example pack the scrollbars on the right and other widgets on the left.
  • Here is the implementation of scrollbars on Frame widget in Python Tkinter.

Full Code Showing the Implementation of Scrollbar on Frame in Python Tkinter

In this code, we have placed scrollbar and Text box widget inside the frame in Python Tkinter. Then by using Pack layout manager we have positioned scrollbar to the Right and Textbox to Left. This can be seen on line 19 and 27. Then finally we have bind the Scrollbar and textbox together, code at line 29, 30.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#5F734C')

frame = Frame(
    ws,
    bg='#A8B9BF'
    )

text_box = Text(
    ws,
    height=13,
    width=32, 
    font=(12)  
)
text_box.pack(side=LEFT,expand=True)
text_box.config(bg='#D9D8D7')

sb_ver = Scrollbar(
    ws,
    orient=VERTICAL
    )

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

text_box.config(yscrollcommand=sb_ver.set)
sb_ver.config(command=text_box.yview)


ws.mainloop()

Output of the above code to show implementation of scrollbar on Frame in Python Tkinter

In this output, Scrollbar is marked on the right. Scrolling the scrollbar will show the text in text box.

Python Tkinter Scrollbar Frame
Python Tkinter Scrollbar Frame

Read: Python Tkinter Button – How to use

Python Tkinter Scrollbar Canvas

Canvas is a free space where diagrams can be drawn. Apart from drawing, we can also place graphics & widgets (frame, text box, buttons, etc) on it.

  • In this section, we will learn how to add a scrollbar on canvas. Also, we’ll create an application to demonstrate Python Tkinter Scrollbar on Canvas.
  • Canvas is used to create a canvas widget in the program followed by the name of the window where you want to place it. Also, provide the width and height of the canvas window.
Canvas(
    ws,
    width=500,
    height=400,
    )
  • We have created a frame and inside that frame we have placed canvas and scrollbar widget.
  • Then we have bind both the scrollbars (horizontal and vertical) with canvas

.Here is the complete source code for Python Tkinter Scrollbar Canvas.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')

frame = Frame(
    ws,
    width=500,
    height=400
    )
frame.pack(expand=True, fill=BOTH)

canvas=Canvas(
    frame,
    bg='#4A7A8C',
    width=500,
    height=400,
    scrollregion=(0,0,700,700)
    )

vertibar=Scrollbar(
    frame,
    orient=VERTICAL
    )
vertibar.pack(side=RIGHT,fill=Y)
vertibar.config(command=canvas.yview)

horibar=Scrollbar(
    frame,
    orient=HORIZONTAL
    )
horibar.pack(side=BOTTOM,fill=X)
horibar.config(command=canvas.xview)

canvas.config(width=500,height=400)

canvas.config(
    xscrollcommand=horibar.set, 
    yscrollcommand=vertibar.set
    )
canvas.pack(expand=True,side=LEFT,fill=BOTH)

ws.mainloop()

Here is the output of the above code to implement Python Tkinter Scrollbar on Canvas

python tkinter scrollbar using canvas
Python Tkinter Scrollbar Canvas

Read: Python Tkinter Radiobutton – How to use

READ:  How to Download Python and Install

Python Tkinter Scrollbar Text box

In this section, we have applied Scrollbar on Text Widget. The output of this program will look similar to Python Tkinter Scrollbar Frame.

  • In Python Tkinter Scrollbar Frame we have applied scrollbar on Frame widget but here we are going to apply it on Text widget directly.
  • We have created a Text widget in Python Tkinter and packed it to LEFT. Then we have created a Scrollbar widget and packed it to RIGHT.
  • The Layout manager used is Pack() code is present on lines 15, 23
from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F25252')

frame = Frame(ws)

text_box = Text(
    ws,
    height=13,
    width=32, 
    font=(12)  
)

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


sb_ver = Scrollbar(
    ws,
    orient=VERTICAL
    )

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

text_box.config(yscrollcommand=sb_ver.set)
sb_ver.config(command=text_box.yview)


ws.mainloop()

Here is the output of the above code to implement the scrollbar on the Text Widget

In this output, you can see that text is displayed on the Text box and a scrollbar is appearing on the right side of the window.

python tkinter scrollbar on textbox
Python Tkinter Scrollbar Text box

Read: Python Tkinter Window Size

Python Tkinter Scrollbar Not Working

In this section, we will see the possible reasons why the scrollbar does not work. Also, we’ll talk about the common errors and their fix while implementing scrollbar in Python Tkinter.

  • Missing Layout manager: First thing to check if you are not seeing the scrollbar on the screen is to look if the scrollbar widget is positioned using layout manager (pack, grid, place).
  • Appearing Small: If the Scrollbar in Python Tkinter is appearing small in that case
    • if you are using Grid layout manager, use sticky and set the scrollbar to NS for vertical Scrollbar and EW for Horizontal Scrollbar.
    • If you are using Pack layout manager, use fill=X for horizontal and fill=Y for Vertical scrollbar(s).

Python Tkinter Scrollbar Text Widget

In this section, we have applied Scrollbar on Text Widget. The output of this program will look similar to Python Tkinter Scrollbar Frame.

  • In Python Tkinter Scrollbar Frame we have applied scrollbar on Frame widget but here we are going to apply it on Text widget directly.
  • We have created a Text widget in Python Tkinter and packed it to LEFT. Then we have created a Scrollbar widget and packed it to RIGHT.
  • The Layout manager used is Pack() code is present on lines 15, 23.

Here is the full code to implement the scrollbar on the Text Widget

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F25252')

text_box = Text(
    ws,
    height=13,
    width=32, 
    font=(12)  
)

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


sb_ver = Scrollbar(
    ws,
    orient=VERTICAL
    )

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

text_box.config(yscrollcommand=sb_ver.set)
sb_ver.config(command=text_box.yview)


ws.mainloop()

Here is the output of the above code to implement the scrollbar on the Text Widget

In this output, you can see that text is displayed on the Text box and a scrollbar is appearing on the right side of the window.

python tkinter scrollbar on textbox
Python Tkinter Scrollbar Text Widget

Read: Python Tkinter Canvas Tutorial

Python Tkinter Scrollbar Orient

Scrollbar in Python Tkinter provides an Orient option using which we can adjust the scrollbar to horizontal to vertical positions.

  • Scrollbars can be applied on widgets like Text box, frame, List box, etc. The requirement could be for vertical scrollbars, horizontal scrollbars, or both.
  • To apply Scrollbars following command is applied. Here lb is a variable for the list box and sb is the variable assigned to the scrollbar.
lb.configure(yscrollcommand=sb.set)
sb.config(command=lb.yview)

Code for Vertical Oriented Scrollbar in Python Tkinter

READ:  How to Convert Pandas Dataframe to Tensor Dataset

In this code, we have set the orientation of the scrollbar to vertical, now users can scroll up and down to access the content. The code can be found in line 19.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#4A7A8C')

frame = Frame(ws)
frame.pack(expand=True, fill=BOTH, padx=20, pady=20)

lb = Listbox(
    frame,
    font= (12)
    )
lb.pack(expand=True, fill=BOTH, side=LEFT)

sb = Scrollbar(
    frame, 
    orient=VERTICAL
    )
sb.pack(fill=Y, side=RIGHT)

lb.configure(yscrollcommand=sb.set)
sb.config(command=lb.yview)

lb.insert(0, 'Fruits')
lb.insert(1,'Citrus – oranges, limes, grapefruits and mandarins')
lb.insert(2,'Stone fruit – apricots, plums, peaches and nectarines')
lb.insert(3,'Tropical and exotic – mangoes a bananasnd ')
lb.insert(4,'Berries – kiwifruit, raspberries, strawberries, blueberries,  and passionfruit')
lb.insert(5,'Melons – honeydew melons, watermelons and rockmelons')
lb.insert(6, 'Vegetables')
lb.insert(7, 'Leafy green – spinach and silverbeet, lettuce')
lb.insert(8, 'Cruciferous – cabbage, broccoli, Brussels sprouts and cauliflower')
lb.insert(9, 'Marrow – zucchini, cucumber and pumpkin')
lb.insert(10, 'Root – yam, sweet potato and potato')
lb.insert(11, 'Edible plant stem – asparagus and celery')
lb.insert(12, 'Allium – garlic, onion and shallot')

ws.mainloop()

Code for Vertical oriented scrollbar in Python Tkinter.

In this output, we have created a vertical oriented scrollbar. The scrollbar will start appearing if the text is greater than the margin of the text holding widget. This way user can scroll up and down to access the text.

python tkinter scrollbar orient vertical
Python Tkinter Scrollbar Orient

Code for Horizontal oriented Scrollbar in Python Tkinter

In this code, we have created a horizontal scrollbar in Python Tkinter. In line 19, you can see that we have set the orientation of the scrollbar to horizontal.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#4A7A8C')

frame = Frame(ws)
frame.pack(expand=True, fill=BOTH, padx=20, pady=20)

lb = Listbox(
    frame,
    font= (12)
    )
lb.pack(expand=True, fill=BOTH)

sb = Scrollbar(
    frame, 
    orient=HORIZONTAL
    )
sb.pack(fill=X)

lb.configure(xscrollcommand=sb.set)
sb.config(command=lb.xview)

lb.insert(0, 'Not all heros wear capes.')
lb.insert(1, 'Game changers are in blue')
lb.insert(2, 'With great power comes great responsibility')
lb.insert(3, 'A noun phrase has a noun or pronoun as the main word')
lb.insert(4, 'With great power comes great responsibility')
lb.insert(5, 'contribute to open source, as this will help & motivate you.')

ws.mainloop()

Output for Horizontal oriented Scrollbar in Python Tkinter

In this output, we have set the orientation of scrollbar to horizontal. Users can now scroll in right and left directions to see the content.

text_box.config(yscrollcommand=sb.set)
sb.config(command=text_box.yview)

You may like the following Python tkinter tutorials:

In this tutorial, we have discussed everything on the Python Tkinter scrollbar and the below points:

  • Python Tkinter Scrollbar Grid
  • Python Tkinter Scrollbar Frame
  • Python Tkinter Scrollbar Canvas
  • Python Tkinter Scrollbar Mouse Wheel
  • Python Tkinter Scrollbar Not Working
  • Python Tkinter Scrollbar Text Widget
  • Python Tkinter Scrollbar Orient