Python Tkinter Scrollbar Widget
In this tutorial, we will cover the Tkinter Scrollbar widget in Python, using which we can add a scrollbar to the user interface of our Tkinter application.
To scroll up or down or right or left the content in a Python desktop application, the Tkinter Scrollbar widget is used.
To scroll the content of other widgets like Listbox, canvas, etc we use this widget.
Both Horizontal and Vertical scrollbars can be created in the Trinket Entry widget.
Below we have an image showing scrollbar widget used with a Listbox:
Tkinter Scrollbar Widget
The syntax of the Scrollbar widget is given below:
W = Scrollbar(master, options)
In the above syntax, the
master parameter denotes the parent window. You can use many options to configure your scrollbar widget and these options are written as comma-separated key-value pairs.
Tkinter Scrollbar Widget Options:
Following are the various options used with Tkinter Scrollbar widgets:
|Name of the Option||Description|
||This option represents the background color of the widget when it is under focus.|
||This option represents the background color of the widget|
||This option represents the border size of the widget. The default value is 2 pixels.|
||With the help of this option, the mouse pointer will be changed to a specific cursor type and it can be an arrow, dot, etc.|
||This option will be set to the procedure associated which is called every time the scrollbar is moved.|
||This option mainly represents the border width around the arrowheads and the slider. The default value of this option is -1.|
||This option represents the thickness of the focus highlights|
||This option indicates the highlight color when the widget is not under the focus|
||This option indicates the highlight color when the widget is under the focus|
||This option is used to control the behavior of the scroll jump. If this option is set to 1, then the callback is called at the time when the user releases the mouse button.|
||This option can be set to either horizontal or vertical depending upon the orientation of the scrollbar.|
||This option represents the width of the scrollbar.|
||This option is used to set the color for the trough|
||By default, you can tab the focus through this widget. If you don't want this behavior you can set this option to 0.|
||This option is mainly used to tell the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. its default value is 300 ms|
||The default value of this option is 100|
Tkinter Scrollbar Widget Methods:
Few methods used with Tkinter Scrollbar widgets are:
This method returns the two numbers suppose
bwhich represents the current position of the scrollbar.
This method is used to connect the scrollbar to any other widget. That is
xscrollcommandof the other widget to this method.
Tkinter Scrollbar Widget Example
Below we have a basic example of a scrollbar widget.
from tkinter import * win= Tk() sbb = Scrollbar(win) sbb.pack(side = RIGHT, fill = Y) mylist = Listbox(win, yscrollcommand = sbb.set) for line in range(45): mylist.insert(END, "Value " + str(line)) mylist.pack(side = LEFT) sbb.config(command = mylist.yview) mainloop()
As you can see in the above code, we have created a Listbox widget with numbers as list items in it. Then we have created a Scrollbar widget and have used the
yscrollcommand option of the Listbox widget to set the Scrollbar widget with it. We have used the Scrollbar widget's
set function here.
So we have covered the Tkinter Scrollbar widget. It is very useful where we have a Tkinter application with long lists or some widget which is too long to fit in the application window. Then we can use the Scrollbar widget for such applications.