Tkinter Scrollbar is a cross-platform GUI (Graphical User Interface) toolkit for Python. It provides a standard way to create GUIs in Python and also lets you use other widgets created by other programmers.
This package relies on the Tcl/Tk libraries, which provide an event-driven interface between its own scripting language and the underlying operating system. The package comes with a large set of GUI elements that are actually widgets, which can be used in building various types of GUIs.
Table of Contents
Horizontal and Vertical Scrollbars: The Entry widget
The Tkinter scrollbar widget is a common way to make it easier for users to scroll down the content on a webpage. When we create horizontal and vertical scrollbars, we can use this widget in Entry widgets. Let’s take a look at how the Entry widget uses the Scrollbar widget to add scrolling functionality.
w = Scrollbar ( master, option, ... )
The parent window is represented by this parameter.
There are a variety of choices, which can be utilized as key-value pairs separated by commas.
This widget can be used with the following commonly used options:
Activebackground: When the widget has the focus, this option is utilized to represent the background color of the widget.
bg: The widget’s backdrop color is represented by this parameter.
bd: The widget’s border width is represented by this parameter.
command: This option can be set to the list’s associated procedure, which will be invoked whenever the scrollbar is moved.
cursor: The mouse pointer is changed to the cursor type specified in this option, which can be an arrow, a dot, or anything else.
elementborderwidth: The border width around the arrow heads and slider is represented by this option. -1 is the default value
Highlightbackground: When the widget doesn’t have the attention, this option is utilised to focus highlighcolor.
highlighcolor: When the widget has the attention, this option is utilised to focus highlighcolor.
highlightthickness: The thickness of the focus highlight is represented by this parameter.
This option is used to regulate how the scroll jump behaves. If it’s set to 1, the callback is triggered when the mouse button is released.
orient: Depending on the scrollbar’s orientation, this option can be set to HORIZONTAL or VERTICAL.
repeatdelay: This setting specifies how long the button must be pressed before the slider begins moving in the same direction repeatedly. The default is 300 milliseconds.
repeatinterval: The repeat interval is set to 100 by default.
takefocus: A scrollbar widget can be used to tab the focus.
troughcolor: The colour of the trough is represented by this parameter.
width: The width of the scrollbar is represented by this option.
The following are the methods used in this widget:
get(): This method returns the two numbers a and b, which represent the scrollbar’s current location.
set(first, last): The scrollbar is connected to the other widget w using this way. To this procedure, pass the other widget’s yscrollcommand or xscrollcommand.
Let’s have a look at an example.
from tkinter import * root = Tk() root.geometry("150x200") w = Label(root, text ='1 to 100 Count Down', font = "50") w.pack() scroll_bar = Scrollbar(root) scroll_bar.pack( side = RIGHT, fill = Y ) mylist = Listbox(root, yscrollcommand = scroll_bar.set ) for line in range(1, 101): mylist.insert(END, str(line)) mylist.pack( side = LEFT, fill = BOTH ) scroll_bar.config( command = mylist.yview ) root.mainloop()
The Entry widget is a powerful and useful way to add scrolling functionality to your webpage. By using the Scrollbar widget, we can create horizontal and vertical scrollbars that work well with this type of entry widget.