Python scrollbar - How do you make a scrollbar in Python?

Published December 16, 2021

Python Scrollbar widget allows you to view all areas of your content by scrolling vertically or horizontally across it. This widget is often used when the presented content exceeds the allotted space.

It should be noted that the Tkinter scrollbar widget is not a component of other widgets such as a Listbox, message, or text widgets; rather, it is a distinct widget on its own.

In this tutorial, we will learn how to make a Tkinter scrollbar widget.

 

Syntax

The syntax for generating a Tkinter scrollbar is very simple:

w = Scrollbar(master, options)

 

The root represents the parent window, and the options represent the widget properties.

 

Tkinter Scrollbar Options

  • Activebackground- defines the color of the widget's background when it focuses.

  • Bg- defines the color of the background.

  • Bd- specifies the size of the scrollbar's border. 

  • Command- used to set up an action linked with the scrollbar's action, such as moving the scrollbar.

  • Cursor- determines how the mouse pointer behaves as it moves across the screen.

  • Element

  • Borderwidth- represents the width of the border surrounding the arrowhead or slider.

  • Highlightbackground- When the widget is not focused, highlightbackground focuses on the highlight color.

  • Highlightcolor- defines the color of the widget's highlight when it is focused.

  • Jump- determines how the scroll jump behaves.

  • Orient- either horizontally or vertically orients the scrollbar.

  • Repeatdelay- defines how long a button is pushed for.

  • Repeatinterval-the repetition interval is set to 100 by default.

  • Takefocus- tabs the widget's focus.

  • Troughcolor- specifies the trough's color.

  • Width-The width of the scrollbar is defined by this option.

 

Tkinter Scrollbar Examples

Vertical Scrollbar

from tkinter import *

root = Tk()

scrollbar = Scrollbar(root)

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

mylist = Listbox(root, yscrollcommand = scrollbar.set )

for line in range(100):

mylist.insert(END, "There are many programming languages " + str(line))

mylist.pack( side = LEFT, fill = BOTH )

scrollbar.config( command = mylist.yview )

mainloop()

 

Python Scrollbar vertical

 

 

Python Horizontal Scrollbar example

from tkinter import *

root = Tk()

scrollbar = Scrollbar(root)

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

mylist = Listbox(root, xscrollcommand = scrollbar.set )

for line in range(100):

mylist.insert(END, "There are many programming languages " + str(line))

mylist.pack( side = TOP, fill = BOTH )

scrollbar.config( command = mylist.yview )

mainloop()

 

output:

Python Horizontal scrollbar

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

490 Views