Python tkinter Scrollbar

A scrollbar is a widget that is useful to scroll the text in another widget.For example, the text in the Text, Canvas, Frame or Listbox can be scrolled from top to bottom or left to right using scrollbars.

tkinter scrollbar types

There are two types of scrollbars.They are horizontal and vertical.The horizontal scrollbar is useful to view the text from left to right. The vertical scrollbar is useful to scroll the text from top to bottom.To create a scrollbar, we have to create Scrollbar class object as:

Horizontal scrollbar

Syntax

CopiedCopy Code
h = Scrollbar(root, orient=HORIZONTAL, bg='green', command=t.xview)

Here, 'h' represents the Scrollbar object which is created as a child to 'root' window.The option 'orient' indicates HORIZONTAL for horizontal scrollbars and VERTICAL indicates vertical scrollbars. 'bg' represents back ground color for the scrollbar.This option may not work in Windows since Window operating system may force some default back ground color for the scrollbar which will not be disturbed by the tkinter. The option 'command' represents the method that is to be executed. The method 'xview' is executed on the object 't'.Here, 't' may represent a widget like Text widget or Listbox.

Vertical scrollbar

Similarly, to create a vertical scrollbar 'v', we can write:

CopiedCopy Code
v = Scrollbar(root, orient=VERTICAL, bg='green', command=t.yview)

After creating the scrollbar, it should be attached to the widget like Text widget or Listbox as:

t.configure(xscrollcommand=h.set)

Here, 't' indicates Text widget.'xscrollcommand' calls the set() method of horizontal scrollbar.In the same way, we can attach vertical scrollbar as:

t.configure(yscrollcommand=v.set)

Finally, the scrollbar should be attached to the root window using the pack() or grid() methods as:

h.pack(side=BOTTOM, fill=X)

Here, we are attaching the horizontal scrollbar at the bottom of the widget and it spreads across X - axis.Similarly, to attach vertical scrollbar, we can use the following statement:

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

Scrollbar Example

A Python program to create a horizontal scrollbar and attach it to a Text widget to view the text from left to right.

CopiedCopy Code

from tkinter import * 
class MyScrollbar: 
   def __init__(self, root):
      self.t = Text(root, width=70, height=15, wrap=NONE) 
      #insert some text into the Text widget 
      for i in range(50): 
         self.t.insert(END, "This is some text") 
      #attach Text widget to root window at the top 
      self.t.pack(side=TOP, fill=X) 
      #create a horizontal scrollbar and attach it to Text widget 
      self.h = Scrollbar(root, orient=HORIZONTAL, command=self.t.xview) 
      #attach Text widget to the horizontal scrollbar 
      self.t.configure(xscrollcommand=self.h.set) 
      #attach Scrollbar to root window at the bottom 
      self.h.pack(side=BOTTOM, fill=X)
root = Tk() 
#create an object to MyScrollbar class 
ms = MyScrollbar(root) 
#the root window handles the mouse click event 
root.mainloop()