Tkinter Scrollbar:

Tkinter Scrollbar is one of the common features of Python GUI’s. The Tkinter Scrollbar is a way to bring the scroll feature to our Python software. This widget is used to add scrolling capability to various widgets, such as list boxes.

Syntax:

The parameter, master represents the parent window and the options are used to set various attribute as key-values separated by commas. The options are described below:

Options of Tkinter Scrollbar:

bg: Shows the color of the slider and arrowheads when the mouse is not over them.

bd: Shows the size of the border around the widget. Default value is 2 pixels around the slider and none around the trough.

activebackground: The color of the slider and arrowheads when the mouse is over them.

command: To change the state of this widget this option is called.

highlightcolor: Show the color of the focus highlight when the scrollbar has the focus.

highlightbackground: The highlight color when the widget is notunder focus.

elementborderwidth: By default is elementborderwidth=-1, which means to use the value of the border-width option.

troughcolor: the color of the trough.

Width: Width of the scrollbar. Default value is 16 pixels.

orient: Use this option to set the alignment. Such as Set orient=HORIZONTAL for a horizontal scrollbar, orient=VERTICAL for a vertical one.

takefocus: Set takefocus=0 to tab focus on the scrollbar.

highlightthickness: The thickness of the highlight, default is 1.

jump: This option controls the behavior of the command option. Moving the slider will trigger the function multiple times. Setting it to 1 only trigger the command once the user releases the scrollbar.

Methods of Tkinter Scrollbar:

set ( first, last ): This method is used to connect a scrollbar to another widget w, set w’s xscrollcommand or yscrollcommand. The arguments have the same meaning as the values returned by the get() method.

get(): This method returns 2 numbers (a, b) describing the current position of the slider. Value a gives the position of the left or top edge of the slider, for horizontal and vertical scrollbars respectively; the value b gives the position of the right or bottom edge.

Example:
#tkinter scrollbar

from tkinter import *
   
head = Tk() 
head.geometry("200x250") 
    
1label = Label(head, text ='TKINTER SCROLLBAR', font = "20")  
1label.pack() 
 
1scroll = Scrollbar(head) 
1scroll.pack(side = LEFT, fill = Y) 
 
1list = Listbox(head, yscrollcommand = myscroll.set )  
for line in range(0, 50): 
    1list.insert(END, "DIGITS" + string(line)) 
1list.pack(side = RIGHT, fill = BOTH )    
 
1scroll.config(command = 1list.yview) 
    
head.mainloop() 
Output:
05 tkinter scrollbar

Comments are closed.