Python Tkinter Scrollbar

python tutorials and learn python

Created with Sketch.

Python Tkinter Scrollbar

The scrollbar widget is used to scroll down the content of the other widgets like listbox, text, and canvas. However, we can also create the horizontal scrollbars to the Entry widget.

The syntax to use the Scrollbar widget is given below.

Syntax

  1. w = Scrollbar(top, options)

A list of possible options is given below.

SNOptionDescription
1activebackgroundThe background color of the widget when it has the focus.
2bgThe background color of the widget.
3bdThe border width of the widget.
4commandIt can be set to the procedure associated with the list which can be called each time when the scrollbar is moved.
5cursorThe mouse pointer is changed to the cursor type set to this option which can be an arrow, dot, etc.
6elementborderwidthIt represents the border width around the arrow heads and slider. The default value is -1.
7HighlightbackgroundThe focus highlighcolor when the widget doesn’t have the focus.
8highlighcolorThe focus highlighcolor when the widget has the focus.
9highlightthicknessIt represents the thickness of the focus highlight.
10jumpIt is used to control the behavior of the scroll jump. If it set to 1, then the callback is called when the user releases the mouse button.
11orientIt can be set to HORIZONTAL or VERTICAL depending upon the orientation of the scrollbar.
12repeatdelayThis option tells the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. The default is 300 ms.
13repeatintervalThe default value of the repeat interval is 100.
14takefocusWe can tab the focus through this widget by default. We can set this option to 0 if we don’t want this behavior.
15troughcolorIt represents the color of the trough.
16widthIt represents the width of the scrollbar.

Methods

The widget provides the following methods.

SNMethodDescription
1get()It returns the two numbers a and b which represents the current position of the scrollbar.
2set(first, last)It is used to connect the scrollbar to the other widget w. The yscrollcommand or xscrollcommand of the other widget to this method.

Example

  1. from tkinter import *
  2. top = Tk()
  3. sb = Scrollbar(top)
  4. sb.pack(side = RIGHT, fill = Y)
  5. mylist = Listbox(top, yscrollcommand = sb.set )
  6. for line in range(30):
  7.     mylist.insert(END, “Number “ + str(line))
  8. mylist.pack( side = LEFT )
  9. sb.config( command = mylist.yview )
  10. mainloop()

Output:

Python Tkinter Scrollbar

 

Leave a Reply

Your email address will not be published. Required fields are marked *